def testLoadManifest(self): self.Touch(self.temp_path, 'runtimes.yaml', contents=ManifestTest.MANIFEST_FILE) self.StartObjectPatch(storage_api.StorageClient, 'ReadObject', side_effect=lambda x: io.BytesIO( ManifestTest.MANIFEST_FILE.encode('utf-8'))) for builder_root in [_URLFromFile(self.temp_path), 'gs://mybucket']: m = runtime_builders.Manifest.LoadFromURI(builder_root + '/runtimes.yaml') self.assertEqual(m._data, yaml.load(ManifestTest.MANIFEST_FILE)) self.assertEqual( set(m.Runtimes()), {'erlang', 'erlang-1', 'erlang-1.2', 'erlang-0', 'erlang-0.1'}) self.assertIsNone(m.GetBuilderReference('foo')) self.assertEqual( m.GetBuilderReference('erlang-1.2'), runtime_builders.BuilderReference( 'erlang-1.2', builder_root + '/erlang-1.2-12345.yaml')) self.assertEqual( m.GetBuilderReference('erlang'), runtime_builders.BuilderReference( 'erlang-1.2', builder_root + '/erlang-1.2-12345.yaml')) self.assertEqual( m.GetBuilderReference('erlang-0'), runtime_builders.BuilderReference('erlang-0.1', None, 'erlang-0.1 is deprecated.'))
def testDeprecationMessage(self): br = runtime_builders.BuilderReference('foo', 'file:///junkpath') br.WarnIfDeprecated() self.AssertErrEquals('') br = runtime_builders.BuilderReference('foo', 'file:///junkpath', 'deprecated') br.WarnIfDeprecated() self.AssertErrEquals('WARNING: deprecated\n')
def testReadError(self): with self.assertRaises(runtime_builders.FileReadError): br = runtime_builders.BuilderReference('foo', 'file:///junkpath') br.LoadCloudBuild({ '_OUTPUT_IMAGE': 'gcr.io/my/image', '_GAE_APPLICATION_YAML_PATH': 'app.yaml' }) with self.assertRaisesRegex( runtime_builders.CloudBuildLoadError, r'There is no build file associated with runtime \[foo\]'): br = runtime_builders.BuilderReference('foo', None) br.LoadCloudBuild({ '_OUTPUT_IMAGE': 'gcr.io/my/image', '_GAE_APPLICATION_YAML_PATH': 'app.yaml' })
def testLegacyDefaultVersion(self): ref = self._GetReferenceFromYaml("""\ env: flex runtime: fortran """) path = _URLFromFile(self.temp_path, 'fortran-3.yaml') self.assertEqual(ref, runtime_builders.BuilderReference('fortran', path))
def testCustom(self): ref = self._GetReferenceFromYaml("""\ env: flex runtime: custom """) path = _URLFromFile(self.temp_path, runtime_builders.Resolver.CLOUDBUILD_FILE) self.assertEqual(ref, runtime_builders.BuilderReference('custom', path))
def testLegacyPinnedVersion(self): ref = self._GetReferenceFromYaml("""\ env: flex runtime: fortran runtime_config: runtime_version: 2 """) path = _URLFromFile(self.temp_path, 'fortran-2.yaml') self.assertEqual(ref, runtime_builders.BuilderReference('fortran', path))
def testPinned(self): ref = self._GetReferenceFromYaml("""\ env: flex runtime: gs://my-bucket/my-builder.yaml """) self.assertEqual( ref, runtime_builders.BuilderReference( 'gs://my-bucket/my-builder.yaml', 'gs://my-bucket/my-builder.yaml'))
def testUnsupportedPath(self): for root in ('bad-url', 'badprotocol://bad-url', 'http://foo'): path = root + '/erlang-v2.yaml' with self.assertRaisesRegex( runtime_builders.InvalidRuntimeBuilderURI, re.escape('[{}] is not a valid runtime builder URI'.format( path))): br = runtime_builders.BuilderReference('foo', path) br.LoadCloudBuild({ '_OUTPUT_IMAGE': 'gcr.io/my/image', '_GAE_APPLICATION_YAML_PATH': 'app.yaml' })
def testGCSFile(self): builder_path = 'gs://mybucket/erlang-v2.yaml' read_object_mock = self.StartObjectPatch( storage_api.StorageClient, 'ReadObject', side_effect=lambda x: io.BytesIO( BuilderReferenceTest.BUILD_FILE.encode('utf-8'))) br = runtime_builders.BuilderReference(builder_path, builder_path) self.assertEqual(br.runtime, builder_path) self.assertEqual( br.LoadCloudBuild({ '_OUTPUT_IMAGE': 'gcr.io/my/image', '_GAE_APPLICATION_YAML_PATH': 'app.yaml' }), self.messages.Build( images=['$_OUTPUT_IMAGE'], steps=[ self.messages.BuildStep( name='gcr.io/my-project/erlang-builder:v2', env=[ 'GAE_APPLICATION_YAML_PATH=' '${_GAE_APPLICATION_YAML_PATH}' ]), self.messages.BuildStep( name='gcr.io/cloud-builders/docker', args=['build', '-t', '$_OUTPUT_IMAGE', '.'], env=[ 'GAE_APPLICATION_YAML_PATH=' '${_GAE_APPLICATION_YAML_PATH}' ]) ], substitutions=self.messages.Build. SubstitutionsValue(additionalProperties=[ self.messages.Build.SubstitutionsValue.AdditionalProperty( key='_GAE_APPLICATION_YAML_PATH', value='app.yaml'), self.messages.Build.SubstitutionsValue.AdditionalProperty( key='_OUTPUT_IMAGE', value='gcr.io/my/image') ]), options=self.messages.BuildOptions( substitutionOption=self.substitution_types.ALLOW_LOOSE), )) config_object = storage_util.ObjectReference.FromUrl(builder_path) read_object_mock.assert_called_once_with(config_object)
def testManifest(self): ref = self._GetReferenceFromYaml("""\ env: flex runtime: erlang """) path = _URLFromFile(self.temp_path, 'erlang-1.yaml') self.assertEqual(ref, runtime_builders.BuilderReference('erlang-1', path)) self.StartObjectPatch( runtime_builders, '_Read', side_effect=runtime_builders.FileReadError('No manifest')) with self.assertRaisesRegex( runtime_builders.BuilderResolveError, r'Unable to resolve a builder for runtime: \[erlang\]'): self._GetReferenceFromYaml("""\ env: flex runtime: erlang """)
def testLocalFile(self): self.Touch(self.temp_path, 'erlang-v2.yaml', contents=BuilderReferenceTest.BUILD_FILE) br = runtime_builders.BuilderReference( 'foo', _URLFromFile(self.temp_path, 'erlang-v2.yaml')) self.assertEqual(br.runtime, 'foo') self.assertEqual( br.LoadCloudBuild({ '_OUTPUT_IMAGE': 'gcr.io/my/image', '_GAE_APPLICATION_YAML_PATH': 'app.yaml' }), self.messages.Build( images=['$_OUTPUT_IMAGE'], steps=[ self.messages.BuildStep( name='gcr.io/my-project/erlang-builder:v2', env=[ 'GAE_APPLICATION_YAML_PATH=' '${_GAE_APPLICATION_YAML_PATH}' ]), self.messages.BuildStep( name='gcr.io/cloud-builders/docker', args=['build', '-t', '$_OUTPUT_IMAGE', '.'], env=[ 'GAE_APPLICATION_YAML_PATH=' '${_GAE_APPLICATION_YAML_PATH}' ]) ], substitutions=self.messages.Build. SubstitutionsValue(additionalProperties=[ self.messages.Build.SubstitutionsValue.AdditionalProperty( key='_GAE_APPLICATION_YAML_PATH', value='app.yaml'), self.messages.Build.SubstitutionsValue.AdditionalProperty( key='_OUTPUT_IMAGE', value='gcr.io/my/image') ]), options=self.messages.BuildOptions( substitutionOption=self.substitution_types.ALLOW_LOOSE), ))