<commit_before>require 'spec_helper'

module Bosh::Director
  describe Api::SnapshotManager do
    let(:cloud) { Config.cloud }
    let(:cloud_factory) { instance_double(Bosh::Director::CloudFactory) }
    let(:username) { 'username-1' }
    let(:time) { Time.now.utc.to_s }

    let(:deployment) { Models::Deployment.make(name: 'deployment') }
    let(:job_queue) { instance_double('Bosh::Director::JobQueue') }
    let(:options) { { foo: 'bar' } }

    before do
      allow(Config).to receive_messages(cloud: cloud)

      # instance 1: one disk with two snapshots
      @instance = Models::Instance.make(vm_cid: 'vm-cid0', agent_id: 'agent0', deployment: deployment, job: 'job', index: 0, uuid: '12abdc456', availability_zone: 'az1')

      @disk = Models::PersistentDisk.make(disk_cid: 'disk0', instance: @instance, active: true)
      Models::Snapshot.make(persistent_disk: @disk, snapshot_cid: 'snap0a', created_at: time, clean: true)
      Models::Snapshot.make(persistent_disk: @disk, snapshot_cid: 'snap0b', created_at: time)

      # instance 2: 1 disk
      instance = Models::Instance.make(vm_cid: 'vm-cid1', agent_id: 'agent1', deployment: deployment, job: 'job', index: 1, uuid: '12xyz456', availability_zone: 'az2')

      disk = Models::PersistentDisk.make(disk_cid: 'disk1', instance: instance, active: true)
      Models::Snapshot.make(persistent_disk: disk, snapshot_cid: 'snap1a', created_at: time)

      # instance 3: no disks
      @instance2 = Models::Instance.make(vm_cid: 'vm-cid2', agent_id: 'agent2', deployment: deployment, job: 'job2', index: 0, uuid: '12def456', availability_zone: 'az3')

      # snapshot from another deployment
      Models::Snapshot.make

      allow(JobQueue).to receive(:new).and_return(job_queue)
      allow(Bosh::Director::CloudFactory).to receive(:new).and_return(cloud_factory)
    end

    let(:task) { instance_double('Bosh::Director::Models::Task', id: 'task_id') }

    describe '#create_deployment_snapshot_task' do
      it 'enqueues a SnapshotDeployment job' do
        expect(job_queue).to receive(:enqueue).with(
          username, Jobs::SnapshotDeployment, 'snapshot deployment', [deployment.name, options], deployment
        ).and_return(task)

        expect(subject.create_deployment_snapshot_task(username, deployment, options)).to eq(task)
      end
    end

    describe '#create_snapshot_task' do
      let(:instance) { instance_double('Bosh::Director::Models::Instance', id: 0) }

      it 'should enqueue a CreateSnapshot job' do
        expect(job_queue).to receive(:enqueue).with(
          username, Jobs::CreateSnapshot, 'create snapshot', [instance.id, options]
        ).and_return(task)

        expect(subject.create_snapshot_task(username, instance, options)).to eq(task)
      end
    end

    describe '#delete_deployment_snapshots_task' do
      it 'enqueues a DeleteDeploymentSnapshots job' do
        expect(job_queue).to receive(:enqueue).with(
          username, Jobs::DeleteDeploymentSnapshots, 'delete deployment snapshots', [deployment.name], deployment
        ).and_return(task)

        expect(subject.delete_deployment_snapshots_task(username, deployment)).to eq(task)
      end
    end

    describe '#delete_snapshots_task' do
      let(:snapshot_cids) { %w[snap0 snap1] }

      it 'enqueues a DeleteSnapshots job' do
        expect(job_queue).to receive(:enqueue).with(
          username, Jobs::DeleteSnapshots, 'delete snapshot', [snapshot_cids]).and_return(task)

        expect(subject.delete_snapshots_task(username, snapshot_cids)).to eq(task)
      end
    end

    describe '#find_by_cid' do
      it 'should return the snapshot with the given id' do
        expect(subject.find_by_cid(deployment, 'snap0a').snapshot_cid).to eq 'snap0a'
      end
    end

    describe '#snapshots' do
      it 'should list all snapshots for a given deployment' do
        response = [
          { 'job' => 'job', 'index' => 0, 'uuid' => '12abdc456','snapshot_cid' => 'snap0a', 'created_at' => time, 'clean' => true },
          { 'job' => 'job', 'index' => 0, 'uuid' => '12abdc456','snapshot_cid' => 'snap0b', 'created_at' => time, 'clean' => false },
          { 'job' => 'job', 'index' => 1, 'uuid' => '12xyz456','snapshot_cid' => 'snap1a', 'created_at' => time, 'clean' => false },
        ]
        expect(subject.snapshots(deployment)).to eq response
      end<commit_msg>Fix flaky postgres snapshot_manager test<commit_after>require 'spec_helper'

module Bosh::Director
  describe Api::SnapshotManager do
    let(:cloud) { Config.cloud }
    let(:cloud_factory) { instance_double(Bosh::Director::CloudFactory) }
    let(:username) { 'username-1' }
    let(:time) { Time.now.utc.to_s }

    let(:deployment) { Models::Deployment.make(name: 'deployment') }
    let(:job_queue) { instance_double('Bosh::Director::JobQueue') }
    let(:options) { { foo: 'bar' } }

    before do
      allow(Config).to receive_messages(cloud: cloud)

      # instance 1: one disk with two snapshots
      @instance = Models::Instance.make(vm_cid: 'vm-cid0', agent_id: 'agent0', deployment: deployment, job: 'job', index: 0, uuid: '12abdc456', availability_zone: 'az1')

      @disk = Models::PersistentDisk.make(disk_cid: 'disk0', instance: @instance, active: true)
      Models::Snapshot.make(persistent_disk: @disk, snapshot_cid: 'snap0a', created_at: time, clean: true)
      Models::Snapshot.make(persistent_disk: @disk, snapshot_cid: 'snap0b', created_at: time)

      # instance 2: 1 disk
      instance = Models::Instance.make(vm_cid: 'vm-cid1', agent_id: 'agent1', deployment: deployment, job: 'job', index: 1, uuid: '12xyz456', availability_zone: 'az2')

      disk = Models::PersistentDisk.make(disk_cid: 'disk1', instance: instance, active: true)
      Models::Snapshot.make(persistent_disk: disk, snapshot_cid: 'snap1a', created_at: time)

      # instance 3: no disks
      @instance2 = Models::Instance.make(vm_cid: 'vm-cid2', agent_id: 'agent2', deployment: deployment, job: 'job2', index: 0, uuid: '12def456', availability_zone: 'az3')

      # snapshot from another deployment
      Models::Snapshot.make

      allow(JobQueue).to receive(:new).and_return(job_queue)
      allow(Bosh::Director::CloudFactory).to receive(:new).and_return(cloud_factory)
    end

    let(:task) { instance_double('Bosh::Director::Models::Task', id: 'task_id') }

    describe '#create_deployment_snapshot_task' do
      it 'enqueues a SnapshotDeployment job' do
        expect(job_queue).to receive(:enqueue).with(
          username, Jobs::SnapshotDeployment, 'snapshot deployment', [deployment.name, options], deployment
        ).and_return(task)

        expect(subject.create_deployment_snapshot_task(username, deployment, options)).to eq(task)
      end
    end

    describe '#create_snapshot_task' do
      let(:instance) { instance_double('Bosh::Director::Models::Instance', id: 0) }

      it 'should enqueue a CreateSnapshot job' do
        expect(job_queue).to receive(:enqueue).with(
          username, Jobs::CreateSnapshot, 'create snapshot', [instance.id, options]
        ).and_return(task)

        expect(subject.create_snapshot_task(username, instance, options)).to eq(task)
      end
    end

    describe '#delete_deployment_snapshots_task' do
      it 'enqueues a DeleteDeploymentSnapshots job' do
        expect(job_queue).to receive(:enqueue).with(
          username, Jobs::DeleteDeploymentSnapshots, 'delete deployment snapshots', [deployment.name], deployment
        ).and_return(task)

        expect(subject.delete_deployment_snapshots_task(username, deployment)).to eq(task)
      end
    end

    describe '#delete_snapshots_task' do
      let(:snapshot_cids) { %w[snap0 snap1] }

      it 'enqueues a DeleteSnapshots job' do
        expect(job_queue).to receive(:enqueue).with(
          username, Jobs::DeleteSnapshots, 'delete snapshot', [snapshot_cids]).and_return(task)

        expect(subject.delete_snapshots_task(username, snapshot_cids)).to eq(task)
      end
    end

    describe '#find_by_cid' do
      it 'should return the snapshot with the given id' do
        expect(subject.find_by_cid(deployment, 'snap0a').snapshot_cid).to eq 'snap0a'
      end
    end

    describe '#snapshots' do
      it 'should list all snapshots for a given deployment' do
        response = [
          { 'job' => 'job', 'index' => 0, 'uuid' => '12abdc456','snapshot_cid' => 'snap0a', 'created_at' => time, 'clean' => true },
          { 'job' => 'job', 'index' => 0, 'uuid' => '12abdc456','snapshot_cid' => 'snap0b', 'created_at' => time, 'clean' => false },
          { 'job' => 'job', 'index' => 1, 'uuid' => '12xyz456','snapshot_cid' => 'snap1a', 'created_at' => time, 'clean' => false },
        ]
        expect(subject.snapshots(deployment)).to match_array response
      end
{'fraction_non_alphanumeric': 0.13967242948134667, 'fraction_numerical': 0.014786169244767972, 'mean_word_length': 3.898607242339833, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7484248', 'n_tokens_mistral': 2843, 'n_tokens_neox': 2758, 'n_words': 619}