Class: Corgibytes::Freshli::Commons::GrpcClient

Inherits:
Object
  • Object
show all
Includes:
RSpec::Matchers
Defined in:
lib/corgibytes/freshli/commons/grpc_client.rb

Overview

Test driver client for communicating with the gRPC API.

Instance Method Summary collapse

Constructor Details

#initialize(port) ⇒ GrpcClient

Returns a new instance of GrpcClient.



19
20
21
# File 'lib/corgibytes/freshli/commons/grpc_client.rb', line 19

def initialize(port)
  @port = port
end

Instance Method Details

#detect_manifests(project_path) ⇒ Object



29
30
31
32
33
34
# File 'lib/corgibytes/freshli/commons/grpc_client.rb', line 29

def detect_manifests(project_path)
  client = grpc_agent_client_on(@captured_port)
  response = client.detect_manifests(::Com::Corgibytes::Freshli::Agent::ProjectLocation.new(path: project_path))

  response.map(&:path)
end

#get_validating_packagesObject

rubocop:disable Naming/AccessorMethodName



37
38
39
40
41
42
# File 'lib/corgibytes/freshli/commons/grpc_client.rb', line 37

def get_validating_packages
  client = grpc_agent_client_on(@port)
  response = client.get_validating_packages(::Google::Protobuf::Empty.new)

  response.map(&:purl)
end

#get_validating_repositoriesObject



44
45
46
47
48
49
# File 'lib/corgibytes/freshli/commons/grpc_client.rb', line 44

def get_validating_repositories
  client = grpc_agent_client_on(@port)
  response = client.get_validating_repositories(::Google::Protobuf::Empty.new)

  response.map(&:url)
end

#health_checkObject



74
75
76
77
78
79
80
81
82
# File 'lib/corgibytes/freshli/commons/grpc_client.rb', line 74

def health_check
  client = Grpc::Health::V1::Health::Stub.new("localhost:#{@port}", :this_channel_is_insecure)
  response = client.check(
    Grpc::Health::V1::HealthCheckRequest.new(
      service: Com::Corgibytes::Freshli::Agent::Agent::Service.service_name
    )
  )
  response.status
end

#is_running!Object

rubocop:disable Naming/PredicateName



85
86
87
# File 'lib/corgibytes/freshli/commons/grpc_client.rb', line 85

def is_running!
  expect(health_check).to eq(:SERVING)
end

#process_manifest(manifest_path, moment_in_time) ⇒ Object

rubocop:enable Naming/AccessorMethodName



52
53
54
55
56
57
58
59
60
61
# File 'lib/corgibytes/freshli/commons/grpc_client.rb', line 52

def process_manifest(manifest_path, moment_in_time)
  client = grpc_agent_client_on(@port)
  response = client.process_manifest(
    ::Com::Corgibytes::Freshli::Agent::ProcessingRequest.new(
      manifest: ::Com::Corgibytes::Freshli::Agent::ManifestLocation.new(path: manifest_path),
      moment: ::Google::Protobuf::Timestamp.from_time(moment_in_time.to_time)
    )
  )
  response.path
end

#retrieve_release_history(package_url) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/corgibytes/freshli/commons/grpc_client.rb', line 63

def retrieve_release_history(package_url)
  client = grpc_agent_client_on(@port)
  response = client.retrieve_release_history(::Com::Corgibytes::Freshli::Agent::Package.new(purl: package_url))
  response.map do |release|
    {
      version: release.version,
      released_at: release.released_at.to_time.to_datetime.new_offset('0:00')
    }
  end
end

#shutdown!Object



23
24
25
26
27
# File 'lib/corgibytes/freshli/commons/grpc_client.rb', line 23

def shutdown!
  client = grpc_agent_client_on(@port)
  response = client.shutdown(::Google::Protobuf::Empty.new)
  expect(response).to be_a(::Google::Protobuf::Empty)
end

#wait_until_running!Object

rubocop:disable Metrics/MethodLength



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/corgibytes/freshli/commons/grpc_client.rb', line 91

def wait_until_running!
  Timeout.timeout(5) do
    attempts = 0
    loop do
      attempts += 1
      yield(attempts) if block_given?

      status = nil
      begin
        status = health_check
      rescue GRPC::Unavailable, GRPC::NotFound
        status = nil
      end

      break if status == :SERVING

      sleep 0.1
    end
  end
end