def testUrl(self): self.assertEqual( service.Service({ "status": { "url": "https://service.example.com" } }).url, "https://service.example.com")
def FormatOutput(self, out, args): if out: svc = service.Service(json.loads(out)) return traffic_pair.GetTrafficTargetPairs(svc.spec_traffic, svc.status_traffic, svc.latest_ready_revision, svc.url) else: raise exceptions.Error('Failed to update traffic for service [{}]'.format( args.service))
def GetTrafficTargetPairsDict(service_dict): """Returns a list of TrafficTargetPairs for a Service as python dictionary. Delegates to GetTrafficTargetPairs(). Args: service_dict: python dict-like object representing a Service unmarshalled from json """ svc = service.Service(service_dict) return GetTrafficTargetPairs(svc.spec_traffic, svc.status_traffic, svc.latest_ready_revision, svc.url)
def testReadySymbolAndColor(self): self.assertEqual( service.Service({ "status": { "latestCreatedRevisionName": "rev1", "latestReadyRevisionName": "rev2", "conditions": [{ "type": "Ready", "status": "False" }] } }).ReadySymbolAndColor(), ("!", "yellow"))
def Transform(self, service_dict): """Transform a service into the output structure of marker classes. Args: service_dict: dictionary object representing a service unmarshalled from json Returns: marker class of the formatted service object. """ record = service.Service(service_dict) fmt = cp.Lines([ k8s_object_printer.FormatHeader(record), k8s_object_printer.FormatLabels(record.labels), ' ', traffic_printer.TransformRouteFields(record), ' ', cp.Labeled([(k8s_object_printer.FormatLastUpdated(record), self._RevisionPrinters(record))]), k8s_object_printer.FormatReadyMessage(record) ]) return fmt
def testStatusTraffic(self): status_traffic = service.Service({ "status": { "traffic": [{ "revisionName": "rev1", "percent": 40, "latestRevision": False, "tag": "tag1", "url": "rev1.service.example.com", }, { "revisionName": "rev2", "percent": 60, "latestRevision": True, "tag": "tag2", "url": "rev2.service.example.com", }] } }).status_traffic self.assertIsNotNone(status_traffic) self.assertIn("LATEST", status_traffic) self.assertIn("rev1", status_traffic)
def FormatOutput(self, out, args): if out: json_object = json.loads(out) return [service.Service(x) for x in json_object] else: raise exceptions.Error('Cannot list services')
KUBERUN_SERVICE_TRAFFIC = service.Service({ "spec": { "traffic": [ { "revisionName": "rev1", "percent": 30, "tag": "tag1", "url": "rev1.service.example.com", }, { "revisionName": "rev2", "latestRevision": False, "percent": 30, "tag": "tag2", "url": "rev2.service.example.com", }, { "configurationName": "conf3", "latestRevision": True, "percent": 40, "tag": "tag3", "url": "conf3.service.example.com", }, ] }, "status": { "traffic": [ { "revisionName": "rev1", "percent": 50, "tag": "tag1", "url": "rev1.service.example.com", }, { "revisionName": "rev2", "latestRevision": False, "percent": 50, "tag": "tag2", "url": "rev2.service.example.com", }, ], } })
"latestReadyRevisionName": "hello-00001-loq", "observedGeneration": 1, "traffic": [ { "revisionName": "hello-00001-loq", "percent": 100, "tag": "tag1", "url": "hello-00001-loq.service.example.com", "latestRevision": false } ], "url": "http://hello.default.example.com" } }""" SERVICE = service.Service(json.loads(SERVICE_STRING)) REVISION_STRING = """{ "metadata": { "namespace": "default", "name": "hello-dxtvy-1", "annotations": { "client.knative.dev/user-image": "gcr.io/knative-samples/helloworld-go" }, "labels": { "labelKey": "labelValue" } }, "spec": { "metadata": {
def testService_consistentWithCloudRun(self): container = runhelpers.Container( "user-container", "gcr.io/knative-samples/helloworld-go", env_vars={"TARGET": "KNATIVE"}, secrets=[{ "name": "mysecret", "secretKey": "secretkey", "secretName": "secretname" }], config_maps=[{ "name": "myconfig", "configKey": "configkey", "configName": "configname" }], command=["run", "this", "container"], args=["use", "these", "arguments"], ports=[{ "name": "http1", "port": 12345, "protocol": "TCP" }], limits={ "cpu": "1000m", "memory": "1.5G" }, volume_mounts=[{ "name": "secretvol", "path": "/path/to/secret" }, { "name": "configmapvol", "path": "/path/to/configmap" }]) spec = runhelpers.RevisionSpec( container, "hello-sa", 300, 5, volumes=[{ "name": "secretvol", "secret": { "secretName": "mySecretName", "items": [{ "key": "secretKey", "path": "foo/bar/secret" }] } }, { "name": "configmapvol", "configMap": { "name": "myconfigMapName", "items": [{ "key": "configMapKey", "path": "foo/bar/configMap" }] } }]) meta = runhelpers.Metadata( "hello-dxtvy-1", "default", annotations={ "client.knative.dev/user-image": "gcr.io/knative-samples/helloworld-go" }, labels={"labelKey": "labelValue"}) rev_msg = run_v1_messages.RevisionTemplate() rev_msg.metadata = meta rev_msg.spec = spec spec_traffic = (runhelpers.TrafficTarget("rev1", 30, None, "tag1", "rev1.service.example.com"), runhelpers.TrafficTarget("rev2", 30, False, "tag2", "rev2.service.example.com"), runhelpers.TrafficTarget("", 40, True, "tag3", "conf3.service.example.com", "conf3")) svc_spec = runhelpers.ServiceSpec(rev_msg, spec_traffic) status_traffic = (runhelpers.TrafficTarget("rev1", 50, None, "tag1", "rev1.service.example.com"), runhelpers.TrafficTarget("rev2", 50, False, "tag2", "rev2.service.example.com")) ready_cond = runhelpers.Condition( "Ready", "false", last_transition_time="2020-02-28T16:09:23Z") conf_cond = runhelpers.Condition( "ConfigurationsReady", "true", last_transition_time="2020-01-31T01:06:31Z") route_cond = runhelpers.Condition( "RoutesReady", last_transition_time="2020-03-31T16:09:23Z") svc_status = runhelpers.ServiceStatus("service-address.example.com", (route_cond, conf_cond, ready_cond), "rev2", "rev2", 42, status_traffic, "service-url.example.com") svc_meta = runhelpers.Metadata( "my-service", "my-namespace", { "serving.knative.dev/creator": "*****@*****.**", "serving.knative.dev/lastModifier": "*****@*****.**" }, {"svc-label": "svc-label-value"}) svc = runhelpers.RunService(runhelpers.TestMessagesModule(), svc_meta, svc_spec, svc_status) properties.VALUES.run.platform.Set(flags.PLATFORM_GKE) run_printer = run_svc_printer.ServicePrinter() run_printer.AddRecord(svc) run_printer.Finish() run_out = self.GetOutput() self.ClearOutput() kuberun_svc = service.Service({ "kind": "Service", "apiVersion": "serving.knative.dev/v1", "metadata": { "name": "my-service", "namespace": "my-namespace", "generation": 42, "annotations": { "serving.knative.dev/creator": "*****@*****.**", "serving.knative.dev/lastModifier": "*****@*****.**" }, "labels": { "svc-label": "svc-label-value" }, }, "spec": { "template": { "metadata": { "name": "hello-dxtvy-1", "annotations": { "client.knative.dev/user-image": "gcr.io/knative-samples/helloworld-go" }, "labels": { "labelKey": "labelValue", } }, "spec": { "containers": [{ "command": ["run", "this", "container"], "args": ["use", "these", "arguments"], "name": "user-container", "image": "gcr.io/knative-samples/helloworld-go", "env": [{ "name": "TARGET", "value": "KNATIVE" }, { "name": "mysecret", "valueFrom": { "secretKeyRef": { "key": "secretkey", "name": "secretname" } } }, { "name": "myconfig", "valueFrom": { "configMapKeyRef": { "key": "configkey", "name": "configname" } } }], "ports": [{ "containerPort": 12345 }], "resources": { "limits": { "cpu": "1000m", "memory": "1.5G", } }, "volumeMounts": [{ "name": "secretvol", "mountPath": "/path/to/secret" }, { "name": "configmapvol", "mountPath": "/path/to/configmap" }] }], "serviceAccountName": "hello-sa", "containerConcurrency": 5, "timeoutSeconds": 300, "volumes": [{ "name": "secretvol", "secret": { "secretName": "mySecretName", "items": [{ "key": "secretKey", "path": "foo/bar/secret" }] } }, { "name": "configmapvol", "configMap": { "name": "myconfigMapName", "items": [{ "key": "configMapKey", "path": "foo/bar/configMap" }] } }] } }, "traffic": [{ "revisionName": "rev1", "percent": 30, "tag": "tag1", "url": "rev1.service.example.com", }, { "revisionName": "rev2", "percent": 30, "latestRevision": False, "tag": "tag2", "url": "rev2.service.example.com", }, { "revisionName": "", "configName": "conf3", "latestRevision": True, "percent": 40, "tag": "tag3", "url": "conf3.service.example.com", }] }, "status": { "observedGeneration": 3, "conditions": [{ "type": "ConfigurationsReady", "status": "True", "lastTransitionTime": "2020-01-31T01:06:31Z" }, { "type": "Ready", "status": "False", "lastTransitionTime": "2020-02-28T16:09:23Z" }, { "type": "RoutesReady", "status": "Unknown", "lastTransitionTime": "2020-03-31T16:09:23Z" }], "latestReadyRevisionName": "rev2", "latestCreatedRevisionName": "rev2", "url": "service-url.example.com", "address": { "url": "service-address.example.com" }, "traffic": [{ "revisionName": "rev1", "percent": 50, "latestRevision": False, "tag": "tag1", "url": "rev1.service.example.com", }, { "revisionName": "rev2", "percent": 50, "tag": "tag2", "url": "rev2.service.example.com", }] } }) self._printer.AddRecord(kuberun_svc) self._printer.Finish() self.AssertOutputEquals(run_out)
def FormatOutput(self, out, args): if out: return service.Service(json.loads(out)) else: raise exceptions.Error('Cannot find service [{}]'.format( args.service))
def testEmptyStatusTraffic(self): status_traffic = service.Service({"status": {}}).status_traffic self.assertIsNotNone(status_traffic) self.assertEqual(dict(), status_traffic)
def testEmptySpecTraffic(self): spec_traffic = service.Service({"spec": {}}).spec_traffic self.assertIsNotNone(spec_traffic) self.assertEqual(dict(), spec_traffic)