def testVolumesSecret(self): s = revision.Revision({ "spec": { "volumes": [{ "name": "myvolumeName", "secret": { "secretName": "mysecret", "items": [{ "key": "mysecretKey", "path": "mysecretPath" }] } }] } }).volumes.secrets self.assertEqual(len(s), 1) self.assertIn("myvolumeName", s) self.assertDictEqual( s["myvolumeName"]._props, { "secretName": "mysecret", "items": [{ "key": "mysecretKey", "path": "mysecretPath" }] })
def testCreationTimestamp(self): self.assertEqual( revision.Revision({ "metadata": { "creationTimestamp": "2019-05-20T18:26:35.210Z" } }).creation_timestamp, "2019-05-20T18:26:35.210Z")
def testVolumeMountsSecret(self): r = revision.Revision({ "spec": { "containers": [{ "volumeMounts": [{ "mountPath": "/my/secret/path", "name": "myvolumeName", }] }], "volumes": [{ "name": "myvolumeName", "secret": { "secretName": "mysecret", "items": [{ "key": "mysecretKey", "path": "mysecretPath" }] } }] } }) s = r.volume_mounts.secrets self.assertEqual(len(s), 1) self.assertIn("/my/secret/path", s) self.assertDictEqual(s, {"/my/secret/path": "myvolumeName"})
def testVolumesConfigMap(self): cm = revision.Revision({ "spec": { "volumes": [{ "name": "myvolumeName", "configMap": { "name": "myconfigMap", "items": [{ "key": "myconfigMapKey", "path": "myconfigMapPath" }] } }] } }).volumes.config_maps self.assertEqual(len(cm), 1) self.assertIn("myvolumeName", cm) self.assertDictEqual( cm["myvolumeName"]._props, { "name": "myconfigMap", "items": [{ "key": "myconfigMapKey", "path": "myconfigMapPath" }] })
def testUserImage_noImage(self): self.assertIsNone( revision.Revision({ "spec": { "containers": [{}] } }).UserImage())
def testVolumeMountsConfigMap(self): cm = revision.Revision({ "spec": { "containers": [{ "volumeMounts": [{ "mountPath": "/my/configMap/path", "name": "myvolumeName", }] }], "volumes": [{ "name": "myvolumeName", "configMap": { "name": "myconfigMap", "items": [{ "key": "myconfigMapKey", "path": "myconfigMapPath" }] } }] } }).volume_mounts.config_maps self.assertEqual(len(cm), 1) self.assertIn("/my/configMap/path", cm) self.assertDictEqual(cm, {"/my/configMap/path": "myvolumeName"})
def testWithBasicRevision(self): rev = revision.Revision({ "metadata": { "namespace": "default", "name": "hello-dxtvy-1", }, "spec": { "metadata": {}, "containers": [{ "name": "user-container", "image": "gcr.io/knative-samples/helloworld-go", "resources": {}, }], "serviceAccountName": "hello-sa", }, "status": { "conditions": [{ "type": "Ready", "status": "true", }] } }) printer = revision_printer.RevisionPrinter() printer.Transform(rev) self.assertIsNone(printer.GetMinInstances(rev)) self.assertIsNone(printer.GetMaxInstances(rev)) self.assertIsNone(printer.GetTimeout(rev)) self.assertEqual(printer.GetUserEnvironmentVariables(rev), []) self.assertEqual(printer.GetSecrets(rev), []) self.assertEqual(printer.GetConfigMaps(rev), [])
def testServiceName(self): self.assertEqual( revision.Revision({ "metadata": { "labels": { "serving.knative.dev/service": "my-svc" } } }).service_name, "my-svc")
def testUserImage_noImageHash(self): self.assertEqual( revision.Revision({ "spec": { "containers": [{ "image": "gcr.us/helloworld" }] } }).UserImage(), "gcr.us/helloworld")
def testActive(self): self.assertEqual( revision.Revision({ "status": { "conditions": [{ "type": "Active", "status": "True" }] } }).active, True)
def testReadySymbolAndColor(self): self.assertEqual( revision.Revision({ "status": { "conditions": [{ "type": "Ready", "status": "False" }] } }).ReadySymbolAndColor(), ("!", "yellow"))
def Transform(self, record): """Transform a revision into the output structure of marker classes.""" rev = revision.Revision(record) fmt = cp.Lines([ k8s_object_printer.FormatHeader(rev), k8s_object_printer.FormatLabels(rev.labels), ' ', self.TransformSpec(rev), k8s_object_printer.FormatReadyMessage(rev) ]) return fmt
def testUserImage_hasImageHashUpToDateServiceAnnotation(self): self.assertEqual( revision.Revision({ "metadata": { "annotations": {} }, "spec": { "containers": [{ "image": "gcr.us/helloworld@sha256:123456789" }] } }).UserImage("gcr.us/helloworld"), "gcr.us/helloworld at 12345678...")
def testUserImage_hasImageHashNoAnnotation(self): self.assertEqual( revision.Revision({ "metadata": { "annotations": {} }, "spec": { "containers": [{ "image": "gcr.us/helloworld@sha256:aaaaaaaaaaaaaaa" }] } }).UserImage(), "gcr.us/helloworld@sha256:aaaaaaaaaaaaaaa")
def testUserImage_hasImageHashOutOfDateAnnotation(self): self.assertEqual( revision.Revision({ "metadata": { "annotations": { "client.knative.dev/user-image": "gcr.us/oldimage", } }, "spec": { "containers": [{ "image": "gcr.us/helloworld@sha256:aaaaaaaaaaaaaaa" }] } }).UserImage(), "gcr.us/helloworld@sha256:aaaaaaaaaaaaaaa")
def testUserImage_hasImageHashUpToDateAnnotationAndOldServiceAnnotation(self): # revision annotation overrides service annotation self.assertEqual( revision.Revision({ "metadata": { "annotations": { "client.knative.dev/user-image": "gcr.us/helloworld" } }, "spec": { "containers": [{ "image": "gcr.us/helloworld@sha256:123456789" }] } }).UserImage("gcr.us/oldimage"), "gcr.us/helloworld at 12345678...")
def testDescribe_Succeed(self): command = '''kuberun core revisions describe hello.example.com --cluster foo --cluster-location us-central1''' expected_out = testdata.REVISION_STRING self.mock_bin_exec.return_value = ( bin_ops.BinaryBackedOperation.OperationResult( command, output=expected_out)) result = self.Run(command) self.AssertExecuteCalledOnce(command_args=[ 'core', 'revisions', 'describe', 'hello.example.com', '--cluster', 'foo', '--cluster-location', 'us-central1' ]) expected_result = revision.Revision(json.loads(expected_out)) self.assertEqual(result, expected_result)
def testUserImage_hasImageHashOldAnnotationAndUpToDateServiceAnnotation(self): # revision annotation overrides service annotation, but considered out of # date self.assertEqual( revision.Revision({ "metadata": { "annotations": { "client.knative.dev/user-image": "gcr.us/oldimage" } }, "spec": { "containers": [{ "image": "gcr.us/helloworld@sha256:aaaaaaaaa" }] } }).UserImage("gcr.us/helloworld"), "gcr.us/helloworld@sha256:aaaaaaaaa")
def template(self): return revision.Revision(self._props['spec']['template'])
def FormatOutput(self, out, args): if out: return revision.Revision(json.loads(out)) else: raise exceptions.Error('Cannot find revision [{}]'.format(args.revision))
def FormatOutput(self, out, args): if out: json_object = json.loads(out) return [revision.Revision(x) for x in json_object] else: raise exceptions.Error('Cannot list revisions')
def testServiceRevisionTemplate_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" }) spec = runhelpers.RevisionSpec(container, "hello-sa", 300, 5) meta = runhelpers.Metadata("hello-dxtvy-1", "default", annotations={ "client.knative.dev/user-image": "gcr.io/knative-samples/helloworld-go" }, labels={"labelKey": "labelValue"}) status = runhelpers.RevisionStatus( [runhelpers.Condition(condition_type="Ready", status="True")]) run_rev = runhelpers.Revision(runhelpers.TestMessagesModule(), meta, spec, status) run_printer = run_rev_printer.RevisionPrinter() run_printer.AddRecord(run_rev) run_printer.Finish() run_out = self.GetOutput() self.ClearOutput() rev = revision.Revision({ "metadata": { "namespace": "default", "name": "hello-dxtvy-1", "annotations": { "client.knative.dev/user-image": "gcr.io/knative-samples/helloworld-go" }, "labels": { "labelKey": "labelValue" } }, "spec": { "metadata": { "annotations": { "autoscaling.knative.dev/minScale": "1", "autoscaling.knative.dev/maxScale": "5", } }, "containers": [{ "name": "user-container", "image": "gcr.io/knative-samples/helloworld-go", "command": [ "run", "this", "container", ], "args": [ "use", "these", "arguments", ], "ports": [{ "containerPort": 12345, "name": "http1", "protocol": "TCP" }], "env": [{ "name": "TARGET", "value": "KNATIVE" }, { "name": "mysecret", "valueFrom": { "secretKeyRef": { "key": "secretkey", "name": "secretname", } } }, { "name": "myconfig", "valueFrom": { "configMapKeyRef": { "key": "configkey", "name": "configname", } } }], "resources": { "limits": { "cpu": "1000m", "memory": "1.5G" } }, }], "serviceAccountName": "hello-sa", "containerConcurrency": 5, "timeoutSeconds": 300 }, "status": { "conditions": [{ "type": "Ready", "status": "true", }] } }) self._printer.AddRecord(rev) self._printer.Finish() self.AssertOutputEquals(run_out)
} } }], "serviceAccountName": "hello-sa", "containerConcurrency": 5, "timeoutSeconds": 300 }, "status": { "conditions": [{ "type": "Ready", "status": "true" }] } }""" REVISION = revision.Revision(json.loads(REVISION_STRING)) DOMAIN_MAPPING_STRING = """ { "metadata":{ "name":"hello.example.com", "namespace":"default", "selfLink":"/apis/domains.cloudrun.com/v1alpha1/namespaces/default/domainmappings/hello.example.com", "uid":"228764ec-9b45-4569-b43d-8928ac9895aa", "resourceVersion":"39712876", "generation":1, "creationTimestamp":"2020-09-22T18:54:54Z", "finalizers":[ "domainmappings.domains.cloudrun.com" ] },