Ryan
Ryan

Reputation: 11

Unable to test labels for Google Cloud Run Service

I've been trying to write unit tests for a Cloud Run Service using Chef Inspec. I've managed to get a few working tests but one of the tests I need is to make sure the Service is labeled properly. I've spent days trying to get this working but I haven't had any success. Every test I try throws an error so I have constructed the following test to try help find a way forward:

title "Test GCP Cloud Run Service"

require "json"
file = File.read("./integration_tests/files/terraform.json")
cloudrun_json = JSON.parse(file)

project_id = input("project_id")
region = input("region")
service_name = cloudrun_json.dig("cloud_run_service", "value", "service_name")
full_service_name = "projects/#{project_id}/locations/#{region}/services/#{service_name}"

control "debug_labels" do
  impact 1.0
  title "Debug Labels Attribute"
  describe google_run_service(name: full_service_name) do
    it "prints the labels attribute" do
      labels = subject.labels
      puts "Available methods for Labels: #{labels.inspect}"
      puts "Available methods for labels: #{labels.methods.sort.inspect}"
    end
  end
end

This is the output of the command when tested against a service that I know has 4 labels:

Labels: #<#<Class:0x000000000a75ed60>::GoogleInSpec::Run::Property::ServiceLabels:0x0000000008fd64b8 @parent_identifier="Service projects/<google-project>/locations/europe-west2/services/<service-name>", @additional_properties=nil>
Available methods for labels: [:!, :!=, :!~, :<=>, :==, :===, :=~, :LittlePlugger, :__id__, :__send__, :additional_properties, :as_null_object, :blank?, :class, :clone, :define_singleton_method, :display, :dup, :enum_for, :eql?, :equal?, :extend, :freeze, :frozen?, :hash, :inspect, :instance_eval, :instance_exec, :instance_of?, :instance_variable_defined?, :instance_variable_get, :instance_variable_set, :instance_variables, :is_a?, :itself, :kind_of?, :method, :methods, :nil?, :null_object?, :object_id, :pretty_inspect, :pretty_print, :pretty_print_cycle, :pretty_print_inspect, :pretty_print_instance_variables, :private_methods, :protected_methods, :public_method, :public_methods, :public_send, :received_message?, :remove_instance_variable, :respond_to?, :send, :should, :should_not, :should_not_receive, :should_receive, :singleton_class, :singleton_method, :singleton_methods, :stub, :stub_chain, :taint, :tainted?, :tap, :then, :to_enum, :to_json, :to_s, :to_yaml, :trust, :unstub, :untaint, :untrust, :untrusted?, :yield_self]

I've tested every useful keyword I could find here to_s, to_yaml and to_json all didn't manage to get any further info out of the labels property. As far as I can tell its empty.

Has anyone managed to get any inspec tests working for Labels on Cloud Run? I am completely out of ideas, feels like the inspec resource is just a dud at this point.

Ideally I would be able to run

control "cloud_run_service" do
  impact 1.0
  title "Check if the Service exists and is healthy"
  describe google_run_service(name: full_service_name) do
    its("labels") { should include "owner" }
  end
end

However, when I try that I get:

×  Service projects/XXXXXX/locations/europe-west2/services/XXXXX labels is expected to include "owner"
     expected #<#<Class:0x00000001108dec10>::GoogleInSpec::Run::Property::ServiceLabels:0x0000000109bc65b0> to include "owner", but it does not respond to `include?`
     Diff:
     @@ -1 +1 @@
     -["owner"]
     +#<#<Class:0x00000001108dec10>::GoogleInSpec::Run::Property::ServiceLabels:0x0000000109bc65b0>

Upvotes: 0

Views: 49

Answers (0)

Related Questions