Class: ServiceForDeploymentMiddlewareTest

Inherits:
Minitest::Test
  • Object
show all
Defined in:
lib/kube/cluster/middleware/service_for_deployment.rb

Constant Summary collapse

Middleware =
Kube::Cluster::Middleware

Instance Method Summary collapse

Instance Method Details

#test_copies_labels_from_sourceObject



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/kube/cluster/middleware/service_for_deployment.rb', line 141

def test_copies_labels_from_source
  m = manifest(Kube::Cluster["Deployment"].new {
    .name = "web"
    .labels = { "app.kubernetes.io/name": "web", "app.kubernetes.io/size": "small" }
    spec.selector.matchLabels = { app: "web" }
    spec.template..labels = { app: "web" }
    spec.template.spec.containers = [
      { name: "web", image: "nginx", ports: [{ name: "http", containerPort: 8080 }] },
    ]
  })

  Middleware::ServiceForDeployment.new.call(m)
  service_labels = m.resources.last.to_h.dig(:metadata, :labels)

  assert_equal "web", service_labels[:"app.kubernetes.io/name"]
  assert_equal "small", service_labels[:"app.kubernetes.io/size"]
end

#test_generates_service_from_deploymentObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/kube/cluster/middleware/service_for_deployment.rb', line 84

def test_generates_service_from_deployment
  m = manifest(Kube::Cluster["Deployment"].new {
    .name = "web"
    .namespace = "production"
    .labels = { app: "web" }
    spec.selector.matchLabels = { app: "web" }
    spec.template..labels = { app: "web" }
    spec.template.spec.containers = [
      { name: "web", image: "nginx", ports: [{ name: "http", containerPort: 8080, protocol: "TCP" }] },
    ]
  })

  Middleware::ServiceForDeployment.new.call(m)

  assert_equal 2, m.resources.size

  deploy, service = m.resources
  assert_equal "Deployment", deploy.to_h[:kind]
  assert_equal "Service", service.to_h[:kind]

  sh = service.to_h
  assert_equal "web", sh.dig(:metadata, :name)
  assert_equal "production", sh.dig(:metadata, :namespace)
  assert_equal({ app: "web" }, sh.dig(:spec, :selector))

  port = sh.dig(:spec, :ports, 0)
  assert_equal "http", port[:name]
  assert_equal 8080, port[:port]
  assert_equal "http", port[:targetPort]
  assert_equal "TCP", port[:protocol]
end

#test_maps_multiple_portsObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/kube/cluster/middleware/service_for_deployment.rb', line 116

def test_maps_multiple_ports
  m = manifest(Kube::Cluster["Deployment"].new {
    .name = "web"
    spec.selector.matchLabels = { app: "web" }
    spec.template..labels = { app: "web" }
    spec.template.spec.containers = [
      {
        name: "web", image: "nginx",
        ports: [
          { name: "http", containerPort: 8080 },
          { name: "metrics", containerPort: 9090 },
        ],
      },
    ]
  })

  Middleware::ServiceForDeployment.new.call(m)
  service = m.resources.last
  ports = service.to_h.dig(:spec, :ports)

  assert_equal 2, ports.size
  assert_equal "http", ports[0][:name]
  assert_equal "metrics", ports[1][:name]
end

#test_skips_deployment_without_match_labelsObject



197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/kube/cluster/middleware/service_for_deployment.rb', line 197

def test_skips_deployment_without_match_labels
  m = manifest(Kube::Cluster["Deployment"].new {
    .name = "web"
    spec.template..labels = { app: "web" }
    spec.template.spec.containers = [
      { name: "web", image: "nginx", ports: [{ name: "http", containerPort: 8080 }] },
    ]
  })

  Middleware::ServiceForDeployment.new.call(m)

  assert_equal 1, m.resources.size
end

#test_skips_deployment_without_named_portsObject



159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/kube/cluster/middleware/service_for_deployment.rb', line 159

def test_skips_deployment_without_named_ports
  m = manifest(Kube::Cluster["Deployment"].new {
    .name = "web"
    spec.selector.matchLabels = { app: "web" }
    spec.template..labels = { app: "web" }
    spec.template.spec.containers = [
      { name: "web", image: "nginx", ports: [{ containerPort: 8080 }] },
    ]
  })

  Middleware::ServiceForDeployment.new.call(m)

  assert_equal 1, m.resources.size
end

#test_skips_deployment_without_portsObject



174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/kube/cluster/middleware/service_for_deployment.rb', line 174

def test_skips_deployment_without_ports
  m = manifest(Kube::Cluster["Deployment"].new {
    .name = "worker"
    spec.selector.matchLabels = { app: "worker" }
    spec.template..labels = { app: "worker" }
    spec.template.spec.containers = [
      { name: "worker", image: "worker:latest" },
    ]
  })

  Middleware::ServiceForDeployment.new.call(m)

  assert_equal 1, m.resources.size
end

#test_skips_non_pod_bearing_resourcesObject



189
190
191
192
193
194
195
# File 'lib/kube/cluster/middleware/service_for_deployment.rb', line 189

def test_skips_non_pod_bearing_resources
  m = manifest(Kube::Cluster["ConfigMap"].new { .name = "config" })

  Middleware::ServiceForDeployment.new.call(m)

  assert_equal 1, m.resources.size
end

#test_works_with_statefulsetObject



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/kube/cluster/middleware/service_for_deployment.rb', line 211

def test_works_with_statefulset
  m = manifest(Kube::Cluster["StatefulSet"].new {
    .name = "db"
    .namespace = "database"
    spec.selector.matchLabels = { app: "db" }
    spec.template..labels = { app: "db" }
    spec.template.spec.containers = [
      { name: "postgres", image: "postgres:16", ports: [{ name: "tcp-pg", containerPort: 5432 }] },
    ]
  })

  Middleware::ServiceForDeployment.new.call(m)

  assert_equal 2, m.resources.size
  assert_equal "Service", m.resources.last.to_h[:kind]
  assert_equal "db", m.resources.last.to_h.dig(:metadata, :name)
  assert_equal "database", m.resources.last.to_h.dig(:metadata, :namespace)
end