# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

project_id = attribute('project_id')

control "gcloud" do
  title "gcloud configuration"
  describe command("gcloud --project=#{project_id} services list --available --format=json") do
    its(:exit_status) { should eq 0 }
    its(:stderr) { should eq '' }

    let!(:data) do
      if subject.exit_status == 0
        JSON.parse(subject.stdout)
      else
        {}
      end
    end

    describe "enabled services" do
      it "includes storage-api" do
        expect(data).to include(
          including(
            "config" => including(
              "name" => "storage-api.googleapis.com",
            ),
          ),
        )
      end
    end
  end
end

# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

project_id = attribute('project_id')

control "gcloud" do
  title "gcloud configuration"
  describe command("gcloud --project=#{project_id} services list --available --format=json") do
    its(:exit_status) { should eq 0 }
    its(:stderr) { should eq '' }

    let(:data) do
      if subject.exit_status == 0
        JSON.parse(subject.stdout)
      else
        {}
      end
    end

    describe "enabled services" do
      it "includes storage-api" do
        expect(data).to include(
          including(
            "config" => including(
              "name" => "storage-api.googleapis.com",
            ),
          ),
        )
      end
    end
  end
end

Use let() instead of let!()
Use let() instead of let!()

This commit changes the sample gcloud integration test from using let!()
(which is evaluated immediately and allows for value changes between
tests) to let() (which is lazy-evaluted and memoized). Unless there's
a reason for allowing the value declared to be changed between tests
it's better to err on the size of memoizing.
