def test_build_go_packages(self): class Options: source = "" go_packages = ["github.com/gotools/vet"] go_importpath = "" go_buildtags = "" plugin = go.GoPlugin("test-part", Options(), self.project_options) os.makedirs(plugin.sourcedir) plugin.pull() os.makedirs(plugin._gopath_bin) os.makedirs(plugin.builddir) # fake some binaries binary = os.path.join(plugin._gopath_bin, "vet") open(binary, "w").close() self.run_mock.reset_mock() plugin.build() self.run_mock.assert_called_once_with( ["go", "build", "-o", binary, plugin.options.go_packages[0]], cwd=plugin._gopath_src, env=mock.ANY, ) self.assertTrue(os.path.exists(plugin._gopath)) self.assertTrue(os.path.exists(plugin._gopath_src)) self.assertTrue(os.path.exists(plugin._gopath_bin)) vet_binary = os.path.join(plugin.installdir, "bin", "vet") self.assertTrue(os.path.exists(vet_binary))
def test_build_with_buildtag(self): class Options: source = 'dir' go_importpath = '' go_packages = [] go_buildtags = ['testbuildtag1', 'testbuildtag2'] plugin = go.GoPlugin('test-part', Options(), self.project_options) os.makedirs(plugin.options.source) os.makedirs(plugin.sourcedir) plugin.pull() os.makedirs(plugin._gopath_bin) os.makedirs(plugin.builddir) self.run_mock.reset_mock() plugin.build() self.run_mock.assert_called_once_with([ 'go', 'install', '-tags=testbuildtag1,testbuildtag2', './dir/...' ], cwd=plugin._gopath_src, env=mock.ANY)
def test_build_with_local_sources(self): class Options: source = 'dir' go_packages = [] go_importpath = '' plugin = go.GoPlugin('test-part', Options(), self.project_options) os.makedirs(plugin.options.source) os.makedirs(plugin.sourcedir) plugin.pull() os.makedirs(plugin._gopath_bin) os.makedirs(plugin.builddir) plugin.build() self.run_mock.assert_has_calls([ mock.call([ 'env', 'GOPATH={}'.format(plugin._gopath), 'go', 'get', '-t', '-d', './dir/...' ], cwd=plugin._gopath_src), mock.call([ 'env', 'GOPATH={}'.format(plugin._gopath), 'go', 'install', './dir/...' ], cwd=plugin._gopath_src), ]) self.assertTrue(os.path.exists(plugin._gopath)) self.assertTrue(os.path.exists(plugin._gopath_src)) self.assertTrue(os.path.exists(plugin._gopath_bin))
def test_build_go_mod(self): class Options: source = "dir" go_channel = "latest/stable" go_packages = [] go_importpath = "" go_buildtags = "" self.run_output_mock.return_value = "go version go13 linux/amd64" plugin = go.GoPlugin("test-part", Options(), self.project) os.makedirs(plugin.builddir) open(os.path.join(plugin.builddir, "go.mod"), "w").close() plugin.build() self.run_output_mock.assert_called_once_with(["go", "version"], cwd=mock.ANY, env=mock.ANY) self.run_mock.assert_called_once_with( ["go", "build", "-o", plugin._install_bin_dir], cwd=plugin.builddir, env=mock.ANY, )
def test_build_with_buildtag(self): class Options: source = 'dir' go_importpath = '' go_packages = [] go_buildtags = ['testbuildtag1', 'testbuildtag2'] plugin = go.GoPlugin('test-part', Options(), self.project_options) os.makedirs(plugin.options.source) os.makedirs(plugin.sourcedir) plugin.pull() os.makedirs(plugin._gopath_bin) os.makedirs(plugin.builddir) self.run_mock.reset_mock() self.run_output_mock.return_value = 'dir/pkg/main main' plugin.build() self.run_output_mock.assert_called_once_with( ['go', 'list', '-f', '{{.ImportPath}} {{.Name}}', './dir/...'], cwd=plugin._gopath_src, env=mock.ANY) binary = os.path.join(plugin._gopath_bin, 'main') self.run_mock.assert_called_once_with([ 'go', 'build', '-o', binary, '-tags=testbuildtag1,testbuildtag2', 'dir/pkg/main' ], cwd=plugin._gopath_src, env=mock.ANY)
def test_build_with_local_sources_and_go_importpath(self): class Options: source = 'dir' go_packages = [] go_importpath = 'github.com/snapcore/launcher' go_buildtags = '' plugin = go.GoPlugin('test-part', Options(), self.project_options) os.makedirs(plugin.options.source) os.makedirs(plugin.sourcedir) plugin.pull() os.makedirs(plugin._gopath_bin) os.makedirs(plugin.builddir) plugin.build() self.run_mock.assert_has_calls([ mock.call([ 'go', 'get', '-t', '-d', './github.com/snapcore/launcher/...' ], cwd=plugin._gopath_src, env=mock.ANY), mock.call(['go', 'install', './github.com/snapcore/launcher/...'], cwd=plugin._gopath_src, env=mock.ANY), ]) self.assertTrue( os.path.exists( os.path.join(plugin._gopath_src, plugin.options.go_importpath)))
def test_build_with_local_sources(self): class Options: source = 'dir' go_packages = [] go_importpath = '' plugin = go.GoPlugin('test-part', Options(), self.project_options) os.makedirs(plugin.options.source) os.makedirs(plugin.sourcedir) plugin.pull() os.makedirs(plugin._gopath_bin) os.makedirs(plugin.builddir) self.run_mock.reset_mock() plugin.build() self.run_mock.assert_called_once_with(['go', 'install', './dir/...'], cwd=plugin._gopath_src, env=mock.ANY) self.assertTrue(os.path.exists(plugin._gopath)) self.assertTrue(os.path.exists(plugin._gopath_src)) self.assertTrue(os.path.exists(plugin._gopath_bin))
def test_cross_compile(self): class Options: source = "" go_packages = ["github.com/gotools/vet"] go_importpath = "" go_buildtags = "" plugin = go.GoPlugin("test-part", Options(), self.project_options) os.makedirs(plugin.sourcedir) plugin.pull() self.assertThat(self.run_mock.call_count, Equals(1)) for call_args in self.run_mock.call_args_list: env = call_args[1]["env"] self.assertIn("CC", env) self.assertThat( env["CC"], Equals("{}-gcc".format(self.project_options.arch_triplet))) self.assertIn("CXX", env) self.assertThat( env["CXX"], Equals("{}-g++".format(self.project_options.arch_triplet))) self.assertIn("CGO_ENABLED", env) self.assertThat(env["CGO_ENABLED"], Equals("1")) self.assertIn("GOARCH", env) self.assertThat(env["GOARCH"], Equals(self.go_arch)) if self.deb_arch == "armhf": self.assertIn("GOARM", env) self.assertThat(env["GOARM"], Equals("7"))
def test_build_packages(self): self.options.go_channel = "" plugin = go.GoPlugin("test-part", self.options, self.project) self.assertThat(plugin.build_packages, Contains("golang-go")) self.assertThat(plugin.build_snaps, Not(Contains("go/latest/stable")))
def test_pull_local_sources(self): class Options: source = "dir" go_channel = "latest/stable" go_packages = [] go_importpath = "" plugin = go.GoPlugin("test-part", Options(), self.project) os.makedirs(plugin.sourcedir) open(os.path.join(plugin.sourcedir, "main.go"), "w").close() plugin.pull() self.run_mock.assert_has_calls([ mock.call( ["go", "get", "-t", "-d", "./dir/..."], cwd=plugin._gopath_src, env=mock.ANY, ) ]) self.assertTrue(os.path.exists(plugin._gopath)) self.assertTrue(os.path.exists(plugin._gopath_src)) self.assertFalse(os.path.exists(plugin._gopath_bin))
def test_build_go_packages(self): class Options: source = "" go_channel = "latest/stable" go_packages = ["github.com/gotools/vet"] go_importpath = "" go_buildtags = "" plugin = go.GoPlugin("test-part", Options(), self.project) os.makedirs(plugin.sourcedir) plugin.pull() os.makedirs(plugin.builddir) self.run_mock.reset_mock() plugin.build() self.run_mock.assert_called_once_with( ["go", "build", plugin.options.go_packages[0]], cwd=os.path.join(plugin.installdir, "bin"), env=mock.ANY, ) self.assert_go_paths(plugin)
def test_clean_build(self): class Options: source = 'dir' go_packages = [] go_importpath = '' go_buildtags = '' plugin = go.GoPlugin('test-part', Options(), self.project_options) os.makedirs(plugin.options.source) os.makedirs(plugin.sourcedir) plugin.pull() os.makedirs(plugin._gopath_bin) os.makedirs(plugin._gopath_pkg) os.makedirs(plugin.builddir) plugin.build() self.assertTrue(os.path.exists(plugin._gopath)) self.assertTrue(os.path.exists(plugin._gopath_src)) self.assertTrue(os.path.exists(plugin._gopath_bin)) plugin.clean_build() self.assertTrue(os.path.exists(plugin._gopath)) self.assertTrue(os.path.exists(plugin._gopath_src)) self.assertFalse(os.path.exists(plugin._gopath_bin)) self.assertFalse(os.path.exists(plugin._gopath_pkg))
def test_clean_build(self): class Options: source = "dir" go_channel = "latest/stable" go_packages = [] go_importpath = "" go_buildtags = "" plugin = go.GoPlugin("test-part", Options(), self.project) plugin.pull() os.makedirs(plugin._gopath_pkg) os.makedirs(plugin.builddir) plugin.build() self.assert_go_paths(plugin) plugin.clean_build() self.assertTrue(os.path.exists(plugin._gopath)) self.assertTrue(os.path.exists(plugin._gopath_src)) self.assertFalse(os.path.exists(plugin._gopath_bin)) self.assertFalse(os.path.exists(plugin._gopath_pkg))
def test_build_go_packages(self): class Options: source = '' go_packages = ['github.com/gotools/vet'] go_importpath = '' go_buildtags = '' plugin = go.GoPlugin('test-part', Options(), self.project_options) os.makedirs(plugin.sourcedir) plugin.pull() os.makedirs(plugin._gopath_bin) os.makedirs(plugin.builddir) # fake some binaries open(os.path.join(plugin._gopath_bin, 'vet'), 'w').close() self.run_mock.reset_mock() plugin.build() self.run_mock.assert_called_once_with( ['go', 'install', plugin.options.go_packages[0]], cwd=plugin._gopath_src, env=mock.ANY) self.assertTrue(os.path.exists(plugin._gopath)) self.assertTrue(os.path.exists(plugin._gopath_src)) self.assertTrue(os.path.exists(plugin._gopath_bin)) vet_binary = os.path.join(plugin.installdir, 'bin', 'vet') self.assertTrue(os.path.exists(vet_binary))
def test_build_with_local_sources(self): class Options: source = 'dir' go_packages = [] go_importpath = '' go_buildtags = '' plugin = go.GoPlugin('test-part', Options(), self.project_options) os.makedirs(plugin.sourcedir) open(os.path.join(plugin.sourcedir, 'main.go'), 'w').close() plugin.pull() os.makedirs(plugin._gopath_bin) os.makedirs(plugin.builddir) self.run_mock.reset_mock() plugin.build() binary = os.path.join(plugin._gopath_bin, 'dir') self.run_mock.assert_called_once_with( ['go', 'build', '-o', binary, './dir/...'], cwd=plugin._gopath_src, env=mock.ANY) self.assertTrue(os.path.exists(plugin._gopath)) self.assertTrue(os.path.exists(plugin._gopath_src)) self.assertTrue(os.path.exists(plugin._gopath_bin))
def test_cross_compile(self): class Options: source = '' go_packages = ['github.com/gotools/vet'] go_importpath = '' go_buildtags = '' plugin = go.GoPlugin('test-part', Options(), self.project_options) os.makedirs(plugin.sourcedir) plugin.pull() self.assertThat(self.run_mock.call_count, Equals(1)) for call_args in self.run_mock.call_args_list: env = call_args[1]['env'] self.assertIn('CC', env) self.assertThat( env['CC'], Equals('{}-gcc'.format(self.project_options.arch_triplet))) self.assertIn('CXX', env) self.assertThat( env['CXX'], Equals('{}-g++'.format(self.project_options.arch_triplet))) self.assertIn('CGO_ENABLED', env) self.assertThat(env['CGO_ENABLED'], Equals('1')) self.assertIn('GOARCH', env) self.assertThat(env['GOARCH'], Equals(self.go_arch)) if self.deb_arch == 'armhf': self.assertIn('GOARM', env) self.assertThat(env['GOARM'], Equals('7'))
def test_build_environment(self): class Options: source = 'dir' go_packages = [] go_importpath = '' plugin = go.GoPlugin('test-part', Options(), self.project_options) os.makedirs(plugin.options.source) os.makedirs(os.path.join(plugin.installdir, 'lib')) os.makedirs(os.path.join(plugin.installdir, 'usr', 'lib')) os.makedirs(os.path.join(plugin.project.stage_dir, 'lib')) os.makedirs(os.path.join(plugin.project.stage_dir, 'usr', 'lib')) plugin.pull() self.assertEqual(1, self.run_mock.call_count) for call_args in self.run_mock.call_args_list: env = call_args[1]['env'] self.assertTrue('GOPATH' in env, 'Expected environment to include GOPATH') self.assertEqual(env['GOPATH'], plugin._gopath) self.assertTrue('CGO_LDFLAGS' in env, 'Expected environment to include CGO_LDFLAGS') expected_flags = [ '-L{}/lib'.format(plugin.installdir), '-L{}/usr/lib'.format(plugin.installdir), '-L{}/lib'.format(plugin.project.stage_dir), '-L{}/usr/lib'.format(plugin.project.stage_dir), ] for flag in expected_flags: self.assertTrue( flag in env['CGO_LDFLAGS'], 'Expected $CGO_LDFLAGS to include {!r}, but it was ' '"{}"'.format(flag, env['CGO_LDFLAGS']))
def test_build_stage_packages_with_go_build_snap_channel(self): class Options: source = "dir" go_importpath = "" build_snaps = ["go/foo"] plugin = go.GoPlugin("test-part", Options(), self.project_options) self.assertNotIn("golang-go", plugin.build_packages)
def test_build_with_local_sources_and_go_importpath(self): class Options: source = "dir" go_channel = "latest/stable" go_packages = [] go_importpath = "github.com/snapcore/launcher" go_buildtags = "" plugin = go.GoPlugin("test-part", Options(), self.project) os.makedirs(plugin.sourcedir) open(os.path.join(plugin.sourcedir, "main.go"), "w").close() plugin.pull() os.makedirs(plugin.builddir) self.run_output_mock.return_value = "github.com/snapcore/launcher main" plugin.build() self.run_output_mock.assert_called_once_with( [ "go", "list", "-f", "{{.ImportPath}} {{.Name}}", "./github.com/snapcore/launcher/...", ], cwd=plugin._gopath_src, env=mock.ANY, ) self.run_mock.assert_has_calls([ mock.call( [ "go", "get", "-t", "-d", "./github.com/snapcore/launcher/..." ], cwd=plugin._gopath_src, env=mock.ANY, ), mock.call( ["go", "build", "github.com/snapcore/launcher"], cwd=os.path.join(plugin.installdir, "bin"), env=mock.ANY, ), ]) self.assertTrue( os.path.exists( os.path.join(plugin._gopath_src, plugin.options.go_importpath))) self.assert_go_paths(plugin)
def test_environment(self): class Options: source = 'http://github.com/testplug' go_packages = [] plugin = go.GoPlugin('test', Options()) self.assertEqual(plugin.env('myroot'), [ 'GOPATH=myroot/go', 'CGO_LDFLAGS="$CGO_LDFLAGS -Lmyroot/lib -Lmyroot/usr/lib ' '-Lmyroot/lib/{0} ' '-Lmyroot/usr/lib/{0} $LDFLAGS"'.format(common.get_arch_triplet()) ])
def test_pull_with_local_sources_or_go_packages(self): class Options: source = None go_channel = "latest/stable" go_packages = [] go_importpath = "" plugin = go.GoPlugin("test-part", Options(), self.project) plugin.pull() self.run_mock.assert_has_calls([]) self.assert_go_paths(plugin)
def test_pull_with_no_local_or_remote_sources(self): class Options: source = None go_packages = [] plugin = go.GoPlugin('test-part', Options()) plugin.pull() self.run_mock.assert_has_calls([]) self.assertTrue(os.path.exists(plugin._gopath)) self.assertTrue(os.path.exists(plugin._gopath_src)) self.assertFalse(os.path.exists(plugin._gopath_bin))
def test_build_classic_dynamic_relink(self, mock_elffile): class Options: source = "" go_channel = "latest/stable" go_packages = ["github.com/gotools/vet"] go_importpath = "" go_buildtags = "" mock_elffile.return_value = MockElfFile(path="foo") self.project.info.confinement = "classic" plugin = go.GoPlugin("test-part", Options(), self.project) os.makedirs(plugin.sourcedir) plugin.pull() os.makedirs(plugin._gopath_bin) os.makedirs(plugin.builddir) # fake some binaries binary = os.path.join(plugin._gopath_bin, "vet") open(binary, "w").close() self.run_mock.reset_mock() plugin.build() self.run_mock.assert_has_calls([ mock.call( ["go", "build", "-o", binary, plugin.options.go_packages[0]], cwd=plugin._gopath_src, env=mock.ANY, ), mock.call( [ "go", "build", "-ldflags", "-linkmode=external", "-o", binary, plugin.options.go_packages[0], ], cwd=plugin._gopath_src, env=mock.ANY, ), ]) self.assertTrue(os.path.exists(plugin._gopath)) self.assertTrue(os.path.exists(plugin._gopath_src)) self.assertTrue(os.path.exists(plugin._gopath_bin)) vet_binary = os.path.join(plugin.installdir, "bin", "vet") self.assertTrue(os.path.exists(vet_binary))
def test_environment(self): class Options: source = 'http://github.com/testplug' go_packages = [] go_importpath = '' plugin = go.GoPlugin('test', Options(), self.project_options) self.assertEqual(plugin.env('myroot'), [ 'GOPATH=myroot/go', 'CGO_LDFLAGS="$CGO_LDFLAGS -Lmyroot/lib -Lmyroot/usr/lib ' '-Lmyroot/lib/{0} ' '-Lmyroot/usr/lib/{0} $LDFLAGS"'.format( plugin.project.arch_triplet) ])
def test_pull_with_local_sources_or_go_packages(self): class Options: source = None go_packages = [] go_importpath = '' plugin = go.GoPlugin('test-part', Options(), self.project_options) plugin.pull() self.run_mock.assert_has_calls([]) self.assertTrue(os.path.exists(plugin._gopath)) self.assertTrue(os.path.exists(plugin._gopath_src)) self.assertFalse(os.path.exists(plugin._gopath_bin))
def test_clean_pull(self): class Options: source = 'dir' go_packages = [] plugin = go.GoPlugin('test-part', Options(), self.project_options) os.makedirs(plugin.options.source) os.makedirs(plugin.sourcedir) plugin.pull() self.assertTrue(os.path.exists(plugin._gopath)) plugin.clean_pull() self.assertFalse(os.path.exists(plugin._gopath))
def test_build_with_local_sources_and_go_importpath(self): class Options: source = 'dir' go_packages = [] go_importpath = 'github.com/snapcore/launcher' go_buildtags = '' plugin = go.GoPlugin('test-part', Options(), self.project_options) os.makedirs(plugin.sourcedir) open(os.path.join(plugin.sourcedir, 'main.go'), 'w').close() plugin.pull() os.makedirs(plugin._gopath_bin) os.makedirs(plugin.builddir) self.run_output_mock.return_value = 'github.com/snapcore/launcher main' plugin.build() self.run_output_mock.assert_called_once_with([ 'go', 'list', '-f', '{{.ImportPath}} {{.Name}}', './github.com/snapcore/launcher/...' ], cwd=plugin._gopath_src, env=mock.ANY) binary = os.path.join(plugin._gopath_bin, 'launcher') self.run_mock.assert_has_calls([ mock.call([ 'go', 'get', '-t', '-d', './github.com/snapcore/launcher/...' ], cwd=plugin._gopath_src, env=mock.ANY), mock.call( ['go', 'build', '-o', binary, 'github.com/snapcore/launcher'], cwd=plugin._gopath_src, env=mock.ANY), ]) self.assertTrue( os.path.exists( os.path.join(plugin._gopath_src, plugin.options.go_importpath)))
def test_clean_pull(self): class Options: source = "dir" go_packages = [] go_importpath = "" go_buildtags = "" plugin = go.GoPlugin("test-part", Options(), self.project_options) os.makedirs(plugin.sourcedir) open(os.path.join(plugin.sourcedir, "main.go"), "w").close() plugin.pull() self.assertTrue(os.path.exists(plugin._gopath)) plugin.clean_pull() self.assertFalse(os.path.exists(plugin._gopath))
def test_no_local_source_with_go_packages(self): class Options: source = None go_packages = ['github.com/gotools/vet'] go_importpath = '' plugin = go.GoPlugin('test-part', Options(), self.project_options) os.makedirs(plugin.sourcedir) plugin.pull() self.run_mock.assert_has_calls([ mock.call(['go', 'get', '-t', '-d', plugin.options.go_packages[0]], env=mock.ANY, cwd=plugin._gopath_src)]) self.assertTrue(os.path.exists(plugin._gopath)) self.assertTrue(os.path.exists(plugin._gopath_src)) self.assertFalse(os.path.exists(plugin._gopath_bin))
def test_clean_pull(self): class Options: source = 'dir' go_packages = [] go_importpath = '' go_buildtags = '' plugin = go.GoPlugin('test-part', Options(), self.project_options) os.makedirs(plugin.sourcedir) open(os.path.join(plugin.sourcedir, 'main.go'), 'w').close() plugin.pull() self.assertTrue(os.path.exists(plugin._gopath)) plugin.clean_pull() self.assertFalse(os.path.exists(plugin._gopath))