diff --git a/app/models/vim_performance_state.rb b/app/models/vim_performance_state.rb index 12c17455afb..8fac038ec5c 100644 --- a/app/models/vim_performance_state.rb +++ b/app/models/vim_performance_state.rb @@ -163,7 +163,7 @@ def container_groups def containers ids = get_assoc(:containers) - ids.empty? ? [] : Container.where(:id => ids).order(:id).to_a + ids.empty? ? [] : Container.where(:id => ids).active_for(timestamp_range).order(:id).to_a end def all_container_groups diff --git a/spec/models/vim_performance_state_spec.rb b/spec/models/vim_performance_state_spec.rb index 1c2a3e0b7ca..9f429d183ab 100644 --- a/spec/models/vim_performance_state_spec.rb +++ b/spec/models/vim_performance_state_spec.rb @@ -61,6 +61,33 @@ end end + describe "#containers" do + it "doesnt return records that are archived before the range" do + container_image = FactoryBot.create(:container_image) + # not deleted is captured + containers = FactoryBot.create_list(:container, 1, :container_image => container_image) + # deleted before is not captured + other_containers = FactoryBot.create_list(:container, 1, :container_image => container_image, :deleted_on => (capture_time - 2.hours)) + # deleted later is captured + containers << FactoryBot.create(:container, :container_image => container_image, :deleted_on => (capture_time + 2.hours)) + + state_data = {:assoc_ids => {:containers => {:on => (containers + other_containers).map(&:id)}}} + actual = described_class.new(:timestamp => capture_time, :state_data => state_data) + expect(actual.containers).to match_array(containers) + end + + it "handles pruned ids" do + container_image = FactoryBot.create(:container_image) + # not deleted is captured + containers = FactoryBot.create_list(:container, 1, :container_image => container_image) + + # add a few ids in here - these represent container ids that are no longer valid (were deleted) + state_data = {:assoc_ids => {:containers => {:on => (containers.map(&:id) + [22, 23, 24, 25, 26, 27])}}} + actual = described_class.new(:timestamp => capture_time, :state_data => state_data) + expect(actual.containers).to match_array(containers) + end + end + describe "#timestamp_range" do let(:timestamp) { Time.now.utc } let(:vim_performance_state) { described_class.new(:timestamp => timestamp) }