func (s *SnapTestSuite) TestSnappyHandleServicesOnInstall(c *C) { snapYamlContent := `name: foo apps: service: command: bin/hello daemon: forking ` si := &snap.SideInfo{ OfficialName: "foo", Revision: 32, } snapPath := makeTestSnapPackage(c, snapYamlContent+"version: 1.0") // revision will be 0 _, err := (&Overlord{}).InstallWithSideInfo(snapPath, si, AllowUnauthenticated, nil) c.Assert(err, IsNil) servicesFile := filepath.Join(dirs.SnapServicesDir, "snap.foo.service.service") c.Assert(osutil.FileExists(servicesFile), Equals, true) st, err := os.Stat(servicesFile) c.Assert(err, IsNil) // should _not_ be executable c.Assert(st.Mode().String(), Equals, "-rw-r--r--") // and that it gets removed on remove snapDir := filepath.Join(dirs.SnapSnapsDir, "foo", "32") yamlPath := filepath.Join(snapDir, "meta", "snap.yaml") snap, err := NewInstalledSnap(yamlPath) c.Assert(err, IsNil) err = (&Overlord{}).Uninstall(snap, &MockProgressMeter{}) c.Assert(err, IsNil) c.Assert(osutil.FileExists(servicesFile), Equals, false) c.Assert(osutil.FileExists(snapDir), Equals, false) }
func (s *SnapTestSuite) TestSnappyHandleBinariesOnInstall(c *C) { snapYamlContent := `name: foo apps: bar: command: bin/bar ` snapPath := makeTestSnapPackage(c, snapYamlContent+"version: 1.0") // revision will be 0 _, err := (&Overlord{}).Install(snapPath, AllowUnauthenticated, nil) c.Assert(err, IsNil) // ensure that the binary wrapper file go generated with the right // name binaryWrapper := filepath.Join(dirs.SnapBinariesDir, "foo.bar") c.Assert(osutil.FileExists(binaryWrapper), Equals, true) // and that it gets removed on remove snapDir := filepath.Join(dirs.SnapSnapsDir, "foo", "0") yamlPath := filepath.Join(snapDir, "meta", "snap.yaml") snap, err := NewInstalledSnap(yamlPath) c.Assert(err, IsNil) err = (&Overlord{}).Uninstall(snap, &MockProgressMeter{}) c.Assert(err, IsNil) c.Assert(osutil.FileExists(binaryWrapper), Equals, false) c.Assert(osutil.FileExists(snapDir), Equals, false) }
func (s *GadgetSuite) TestCleanupGadgetHardwareRules(c *C) { info, err := snap.InfoFromSnapYaml(hardwareYaml) c.Assert(err, IsNil) err = writeApparmorAdditionalFile(info) c.Assert(err, IsNil) additionalFile := filepath.Join(dirs.SnapAppArmorDir, "device-hive-iot-hal.json.additional") c.Assert(osutil.FileExists(additionalFile), Equals, true) err = cleanupGadgetHardwareUdevRules(info) c.Assert(err, IsNil) c.Assert(osutil.FileExists(additionalFile), Equals, false) }
func populateStateFromInstalled() error { all, err := (&snappy.Overlord{}).Installed() if err != nil { return err } if osutil.FileExists(dirs.SnapStateFile) { return fmt.Errorf("cannot create state: state %q already exists", dirs.SnapStateFile) } st := state.New(&overlordStateBackend{ path: dirs.SnapStateFile, }) st.Lock() defer st.Unlock() for _, sn := range all { // no need to do a snapstate.Get() because this is firstboot info := sn.Info() var snapst snapstate.SnapState snapst.Sequence = append(snapst.Sequence, &info.SideInfo) snapst.Channel = info.Channel snapst.Active = sn.IsActive() snapstate.Set(st, sn.Name(), &snapst) } return nil }
func (s *SnapTestSuite) TestDesktopFileIsAddedAndRemoved(c *C) { yamlFile, err := makeInstalledMockSnap(string(desktopAppYaml), 11) c.Assert(err, IsNil) snap, err := NewInstalledSnap(yamlFile) c.Assert(err, IsNil) // create a mock desktop file err = os.MkdirAll(filepath.Join(filepath.Dir(yamlFile), "gui"), 0755) c.Assert(err, IsNil) err = ioutil.WriteFile(filepath.Join(filepath.Dir(yamlFile), "gui", "foobar.desktop"), []byte(mockDesktopFile), 0644) c.Assert(err, IsNil) // ensure that activate creates the desktop file err = ActivateSnap(snap, nil) c.Assert(err, IsNil) mockDesktopFilePath := filepath.Join(dirs.SnapDesktopFilesDir, "foo_foobar.desktop") content, err := ioutil.ReadFile(mockDesktopFilePath) c.Assert(err, IsNil) c.Assert(string(content), Equals, ` [Desktop Entry] Name=foo Icon=/snap/foo/11/foo.png`) // unlink (deactivate) removes it again err = UnlinkSnap(snap.Info(), nil) c.Assert(err, IsNil) c.Assert(osutil.FileExists(mockDesktopFilePath), Equals, false) }
func (dbs *databaseSuite) TestGenerateKey(c *C) { fingerp, err := dbs.db.GenerateKey("account0") c.Assert(err, IsNil) c.Check(fingerp, NotNil) keyPath := filepath.Join(dbs.topDir, "private-keys-v0/account0", fingerp) c.Check(osutil.FileExists(keyPath), Equals, true) }
func UndoSetupSnap(s snap.PlaceInfo, meter progress.Meter) { // SetupSnap did it not made far enough if !osutil.FileExists(s.MountDir()) { return } if err := RemoveSnapFiles(s, meter); err != nil { logger.Noticef("cannot remove snap files: %s", err) } mountDir := s.MountDir() snapPath := s.MountFile() // remove install dir and the snap blob itself for _, path := range []string{ mountDir, snapPath, } { if err := os.RemoveAll(path); err != nil { logger.Noticef("cannot remove snap package at %v: %s", mountDir, err) } } // FIXME: do we need to undo installGadgetHardwareUdevRules via // cleanupGadgetHardwareUdevRules ? it will go away // and can only be used during install right now }
// InDeveloperMode returns true if the image was build with --developer-mode func InDeveloperMode() bool { // FIXME: this is a bit terrible, we really need a single // bootloader dir like /boot or /boot/loader // instead of having to query the partition code bootloader, err := findBootloader() if err != nil { // can only happy on systems like ubuntu classic // that are not full snappy systems return false } file := filepath.Join(bootloader.Dir(), InstallYamlFile) if !osutil.FileExists(file) { // no idea return false } InstallYaml, err := parseInstallYaml(file) if err != nil { // no idea return false } return InstallYaml.InstallOptions.DeveloperMode }
func (s *SquashfsTestSuite) TestInstallFailUnmountsSnap(c *C) { c.Skip("no easy path to this kind of late verification failure now!") snapPkg := makeTestSnapPackage(c, `name: hello version: 1.10 apps: some-binary: command: some-binary plugs: [some-binary] plugs: some-binary: interface: old-security security-template: not-there `) // install but our missing security-template will break the install // revision will be 0 _, err := (&Overlord{}).Install(snapPkg, 0, &MockProgressMeter{}) c.Assert(err, ErrorMatches, "could not find specified template: not-there.*") // ensure the mount unit is not there mup := systemd.MountUnitPath("/snap/hello/1.10", "mount") c.Assert(osutil.FileExists(mup), Equals, false) // ensure that the mount gets unmounted and stopped c.Assert(s.systemdCmds, DeepEquals, [][]string{ {"start", "snap-hello-0.mount"}, {"--root", dirs.GlobalRootDir, "disable", "snap-hello-0.mount"}, {"stop", "snap-hello-0.mount"}, {"show", "--property=ActiveState", "snap-hello-0.mount"}, }) }
// newUboot create a new Uboot bootloader object func newUboot() Bootloader { u := &uboot{} if !osutil.FileExists(u.envFile()) { return nil } return u }
func (t *CreateTestSuite) TestCreateFailDestroys(c *C) { t.makeMockLxdServer(c) t.imageReader = strings.NewReader("its all broken") err := Create(&progress.NullProgress{}) c.Assert(err, ErrorMatches, `(?m)failed to unpack .*`) c.Assert(osutil.FileExists(dirs.ClassicDir), Equals, false) }
// Release returns the release of the current snappy image func Release(c *check.C) string { // FIXME: use `snap info` once it is available again if osutil.FileExists(snappyCmd) { return "15.04" } return "rolling" }
// newGrub create a new Grub bootloader object func newGrub() Bootloader { g := &grub{} if !osutil.FileExists(g.configFile()) { return nil } return g }
func (s *SnapTestSuite) TestWriteHardwareUdevEtc(c *C) { info, err := snap.InfoFromSnapYaml(hardwareYaml) c.Assert(err, IsNil) dirs.SnapUdevRulesDir = c.MkDir() writeGadgetHardwareUdevRules(info) c.Assert(osutil.FileExists(filepath.Join(dirs.SnapUdevRulesDir, "80-snappy_gadget-foo_device-hive-iot-hal.rules")), Equals, true) }
func getTrustedAccountKey() string { if !osutil.FileExists(dirs.SnapTrustedAccountKey) { // XXX: allow this fallback here for integration tests, // until we have a proper trusted public key shared // with the store and decide possibly for a different strategy return os.Getenv("SNAPPY_TRUSTED_ACCOUNT_KEY") } return dirs.SnapTrustedAccountKey }
func (s *SnapTestSuite) TestWriteHardwareUdevCleanup(c *C) { info, err := snap.InfoFromSnapYaml(hardwareYaml) c.Assert(err, IsNil) dirs.SnapUdevRulesDir = c.MkDir() udevRulesFile := filepath.Join(dirs.SnapUdevRulesDir, "80-snappy_gadget-foo_device-hive-iot-hal.rules") c.Assert(ioutil.WriteFile(udevRulesFile, nil, 0644), Equals, nil) cleanupGadgetHardwareUdevRules(info) c.Assert(osutil.FileExists(udevRulesFile), Equals, false) }
func (s *SquashfsTestSuite) TestRemoveViaSquashfsWorks(c *C) { snapPath := makeTestSnapPackage(c, packageHello) si := &snap.SideInfo{ OfficialName: "hello-snap", Revision: 16, } snap, err := (&Overlord{}).InstallWithSideInfo(snapPath, si, 0, &MockProgressMeter{}) c.Assert(err, IsNil) installedSnap, err := NewInstalledSnap(filepath.Join(snap.MountDir(), "meta", "snap.yaml")) c.Assert(err, IsNil) // after install the blob is in the right dir c.Assert(osutil.FileExists(filepath.Join(dirs.SnapBlobDir, "hello-snap_16.snap")), Equals, true) // now remove and ensure its gone err = (&Overlord{}).Uninstall(installedSnap, &MockProgressMeter{}) c.Assert(err, IsNil) c.Assert(osutil.FileExists(filepath.Join(dirs.SnapBlobDir, "hello-snap_16.snap")), Equals, false) }
func parseSnapYamlFile(yamlPath string) (*snapYaml, error) { yamlData, err := ioutil.ReadFile(yamlPath) if err != nil { return nil, err } // legacy support sucks :-/ hasConfig := osutil.FileExists(filepath.Join(filepath.Dir(yamlPath), "hooks", "config")) return parseSnapYamlData(yamlData, hasConfig) }
func loadState(backend state.Backend) (*state.State, error) { if !osutil.FileExists(dirs.SnapStateFile) { return state.New(backend), nil } r, err := os.Open(dirs.SnapStateFile) if err != nil { return nil, fmt.Errorf("failed to read the state file: %s", err) } defer r.Close() return state.ReadState(backend, r) }
func (s *SnapTestSuite) TestAddPackageDesktopFiles(c *C) { expectedDesktopFilePath := filepath.Join(dirs.SnapDesktopFilesDir, "foo_foobar.desktop") c.Assert(osutil.FileExists(expectedDesktopFilePath), Equals, false) yamlFile, err := makeInstalledMockSnap(desktopAppYaml, 11) c.Assert(err, IsNil) snap, err := NewInstalledSnap(yamlFile) c.Assert(err, IsNil) // generate .desktop file in the package baseDir baseDir := snap.Info().MountDir() err = os.MkdirAll(filepath.Join(baseDir, "meta", "gui"), 0755) c.Assert(err, IsNil) err = ioutil.WriteFile(filepath.Join(baseDir, "meta", "gui", "foobar.desktop"), mockDesktopFile, 0644) c.Assert(err, IsNil) err = addPackageDesktopFiles(snap.Info()) c.Assert(err, IsNil) c.Assert(osutil.FileExists(expectedDesktopFilePath), Equals, true) }
func (s *SnapTestSuite) TestRemovePackageDesktopFiles(c *C) { mockDesktopFilePath := filepath.Join(dirs.SnapDesktopFilesDir, "foo_foobar.desktop") err := os.MkdirAll(dirs.SnapDesktopFilesDir, 0755) c.Assert(err, IsNil) err = ioutil.WriteFile(mockDesktopFilePath, mockDesktopFile, 0644) c.Assert(err, IsNil) snap, err := snap.InfoFromSnapYaml([]byte(desktopAppYaml)) c.Assert(err, IsNil) err = removePackageDesktopFiles(snap) c.Assert(err, IsNil) c.Assert(osutil.FileExists(mockDesktopFilePath), Equals, false) }
func (s *SquashfsTestSuite) TestRemoveSquashfsMountUnit(c *C) { info := &snap.Info{ SideInfo: snap.SideInfo{ OfficialName: "foo", Revision: 13, }, Version: "1.1", Architectures: []string{"all"}, } inter := &MockProgressMeter{} err := addSquashfsMount(info, true, inter) c.Assert(err, IsNil) // ensure we have the files p := filepath.Join(dirs.SnapServicesDir, "snap-foo-13.mount") c.Assert(osutil.FileExists(p), Equals, true) // now call remove and ensure they are gone err = removeSquashfsMount(info.MountDir(), inter) c.Assert(err, IsNil) p = filepath.Join(dirs.SnapServicesDir, "snaps-foo-13.mount") c.Assert(osutil.FileExists(p), Equals, false) }
func (s *SquashfsTestSuite) TestInstallKernelSnapRemovesKernelAssets(c *C) { files := [][]string{ {"vmlinuz-4.2", "I'm a kernel"}, {"initrd.img-4.2", "...and I'm an initrd"}, } snapPkg := makeTestSnapPackageWithFiles(c, packageKernel, files) si := &snap.SideInfo{ OfficialName: "ubuntu-kernel", Revision: 42, } snap, err := (&Overlord{}).InstallWithSideInfo(snapPkg, si, 0, &MockProgressMeter{}) c.Assert(err, IsNil) installedSnap, err := NewInstalledSnap(filepath.Join(snap.MountDir(), "meta", "snap.yaml")) c.Assert(err, IsNil) installedSnap.isActive = false kernelAssetsDir := filepath.Join(s.bootloader.Dir(), "ubuntu-kernel_42.snap") c.Assert(osutil.FileExists(kernelAssetsDir), Equals, true) // ensure uninstall cleans the kernel assets err = (&Overlord{}).Uninstall(installedSnap, &MockProgressMeter{}) c.Assert(err, IsNil) c.Assert(osutil.FileExists(kernelAssetsDir), Equals, false) }
func (cts *ConfigTestSuite) TestNetworkSetEmptyRemoves(c *C) { path := filepath.Join(interfacesRoot, "eth0") content := "auto eth0" err := ioutil.WriteFile(path, []byte(content), 0644) c.Assert(err, IsNil) // empty content removes nc := []passthroughConfig{ {Name: "eth0", Content: ""}, } err = setInterfaces(nc) c.Assert(err, IsNil) _, err = ioutil.ReadFile(path) c.Assert(osutil.FileExists(path), Equals, false) }
func (s *SnapTestSuite) TestLocalSnapInstallWithBlessedMetadataOverridingName(c *C) { snapPath := makeTestSnapPackage(c, "") si := &snap.SideInfo{ OfficialName: "bar", Revision: 55, } snap, err := (&Overlord{}).InstallWithSideInfo(snapPath, si, 0, nil) c.Assert(err, IsNil) c.Check(snap.Name(), Equals, "bar") c.Check(snap.Revision, Equals, 55) baseDir := filepath.Join(dirs.SnapSnapsDir, "bar", "55") c.Assert(osutil.FileExists(baseDir), Equals, true) }
func (s *SquashfsTestSuite) TestUnpackGlob(c *C) { data := "some random data" snap := makeSnap(c, "", data) outputDir := c.MkDir() err := snap.Unpack("data*", outputDir) c.Assert(err, IsNil) // this is the file we expect content, err := ioutil.ReadFile(filepath.Join(outputDir, "data.bin")) c.Assert(err, IsNil) c.Assert(string(content), Equals, data) // ensure glob was honored c.Assert(osutil.FileExists(filepath.Join(outputDir, "meta/snap.yaml")), Equals, false) }
func (s *SquashfsTestSuite) TestInstallKernelSnapNoUnpacksKernelForGrub(c *C) { // pretend to be a grub system origGetGadget := getGadget s.AddCleanup(func() { getGadget = origGetGadget }) getGadget = getFakeGrubGadget files := [][]string{ {"vmlinuz-4.2", "I'm a kernel"}, } snapPkg := makeTestSnapPackageWithFiles(c, packageKernel, files) _, err := (&Overlord{}).Install(snapPkg, 0, &MockProgressMeter{}) c.Assert(err, IsNil) // kernel is *not* here vmlinuz := filepath.Join(s.bootloader.Dir(), "ubuntu-kernel_4.0-1.snap", "vmlinuz") c.Assert(osutil.FileExists(vmlinuz), Equals, false) }
func (t *CreateTestSuite) TestCreate(c *C) { t.makeMockLxdServer(c) err := Create(&progress.NullProgress{}) c.Assert(err, IsNil) c.Assert(t.runInChroot, DeepEquals, [][]string{ {"deluser", "ubuntu"}, {"apt-get", "install", "-y", "libnss-extrausers"}, }) c.Assert(t.getgrnamCalled, DeepEquals, []string{"sudo"}) for _, canary := range []string{"/etc/nsswitch.conf", "/etc/hosts", "/usr/sbin/policy-rc.d"} { c.Assert(osutil.FileExists(filepath.Join(dirs.ClassicDir, canary)), Equals, true) } leftovers, err := filepath.Glob(filepath.Join(os.TempDir(), "classic*")) c.Assert(err, IsNil) c.Assert(leftovers, HasLen, 0) }
func (s *SnapTestSuite) TestLocalSnapInstall(c *C) string { snapPath := makeTestSnapPackage(c, "") // revision will be 0 snap, err := (&Overlord{}).Install(snapPath, 0, nil) c.Assert(err, IsNil) c.Check(snap.Name(), Equals, "foo") baseDir := filepath.Join(dirs.SnapSnapsDir, fooComposedName, "0") c.Assert(osutil.FileExists(baseDir), Equals, true) snapEntries := listDir(c, filepath.Join(dirs.SnapSnapsDir, fooComposedName)) c.Check(snapEntries, DeepEquals, []string{"0", "current"}) snapDataEntries := listDir(c, filepath.Join(dirs.SnapDataDir, fooComposedName)) c.Check(snapDataEntries, DeepEquals, []string{"0", "current"}) return snapPath }
func (t *remoteRepoTestSuite) TestDownloadFails(c *C) { var tmpfile *os.File download = func(name string, w io.Writer, req *http.Request, pbar progress.Meter) error { tmpfile = w.(*os.File) return fmt.Errorf("uh, it failed") } snap := &snap.Info{} snap.OfficialName = "foo" snap.AnonDownloadURL = "anon-url" snap.DownloadURL = "AUTH-URL" // simulate a failed download path, err := t.store.Download(snap, nil, nil) c.Assert(err, ErrorMatches, "uh, it failed") c.Assert(path, Equals, "") // ... and ensure that the tempfile is removed c.Assert(osutil.FileExists(tmpfile.Name()), Equals, false) }