示例#1
0
func TestMapFilesToFilesystems(t *testing.T) {
	type in struct {
		config types.Config
	}
	type out struct {
		files map[types.Filesystem][]types.File
		err   error
	}

	tests := []struct {
		in  in
		out out
	}{
		{
			in:  in{config: types.Config{}},
			out: out{files: map[types.Filesystem][]types.File{}},
		},
		{
			in:  in{config: types.Config{Storage: types.Storage{Files: []types.File{{Filesystem: "foo"}}}}},
			out: out{err: ErrFilesystemUndefined},
		},
		{
			in: in{config: types.Config{Storage: types.Storage{
				Filesystems: []types.Filesystem{{Name: "fs1"}},
				Files:       []types.File{{Filesystem: "fs1", Path: "/foo"}, {Filesystem: "fs1", Path: "/bar"}},
			}}},
			out: out{files: map[types.Filesystem][]types.File{types.Filesystem{Name: "fs1"}: {{Filesystem: "fs1", Path: "/foo"}, {Filesystem: "fs1", Path: "/bar"}}}},
		},
		{
			in: in{config: types.Config{Storage: types.Storage{
				Filesystems: []types.Filesystem{{Name: "fs1", Path: "/fs1"}, {Name: "fs2", Path: "/fs2"}},
				Files:       []types.File{{Filesystem: "fs1", Path: "/foo"}, {Filesystem: "fs2", Path: "/bar"}},
			}}},
			out: out{files: map[types.Filesystem][]types.File{
				types.Filesystem{Name: "fs1", Path: "/fs1"}: {{Filesystem: "fs1", Path: "/foo"}},
				types.Filesystem{Name: "fs2", Path: "/fs2"}: {{Filesystem: "fs2", Path: "/bar"}},
			}},
		},
		{
			in: in{config: types.Config{Storage: types.Storage{
				Filesystems: []types.Filesystem{{Name: "fs1"}, {Name: "fs1", Path: "/fs1"}},
				Files:       []types.File{{Filesystem: "fs1", Path: "/foo"}, {Filesystem: "fs1", Path: "/bar"}},
			}}},
			out: out{files: map[types.Filesystem][]types.File{
				types.Filesystem{Name: "fs1", Path: "/fs1"}: {{Filesystem: "fs1", Path: "/foo"}, {Filesystem: "fs1", Path: "/bar"}},
			}},
		},
	}

	for i, test := range tests {
		logger := log.New()
		files, err := stage{Util: util.Util{Logger: &logger}}.mapFilesToFilesystems(test.in.config)
		if !reflect.DeepEqual(test.out.err, err) {
			t.Errorf("#%d: bad error: want %v, got %v", i, test.out.err, err)
		}
		if !reflect.DeepEqual(test.out.files, files) {
			t.Errorf("#%d: bad map: want %#v, got %#v", i, test.out.files, files)
		}
	}
}
func TestUserLookup(t *testing.T) {
	if os.Geteuid() != 0 {
		t.Skip("test requires root for chroot(), skipping")
	}

	// perform a user lookup to ensure libnss_files.so is loaded
	// note this assumes /etc/nsswitch.conf invokes files.
	user.Lookup("root")

	td, err := tempBase()
	if err != nil {
		t.Fatalf("temp base error: %v", err)
	}

	logger := log.New()
	defer logger.Close()

	u := &Util{
		DestDir: td,
		Logger:  &logger,
	}

	usr, err := u.userLookup("foo")
	if err != nil {
		t.Fatalf("lookup error: %v", err)
	}

	if usr.Name != "foo" {
		t.Fatalf("unexpected name: %q", usr.Name)
	}

	if usr.Uid != "44" {
		t.Fatalf("unexpected uid: %q", usr.Uid)
	}

	if usr.Gid != "4242" {
		t.Fatalf("unexpected gid: %q", usr.Gid)
	}
}
示例#3
0
func main() {
	flags := struct {
		clearCache    bool
		configCache   string
		onlineTimeout time.Duration
		oem           oem.Name
		root          string
		stage         stages.Name
		version       bool
	}{}

	flag.BoolVar(&flags.clearCache, "clear-cache", false, "clear any cached config")
	flag.StringVar(&flags.configCache, "config-cache", "/run/ignition.json", "where to cache the config")
	flag.DurationVar(&flags.onlineTimeout, "online-timeout", exec.DefaultOnlineTimeout, "how long to wait for a provider to come online")
	flag.Var(&flags.oem, "oem", fmt.Sprintf("current oem. %v", oem.Names()))
	flag.StringVar(&flags.root, "root", "/", "root of the filesystem")
	flag.Var(&flags.stage, "stage", fmt.Sprintf("execution stage. %v", stages.Names()))
	flag.BoolVar(&flags.version, "version", false, "print the version and exit")

	flag.Parse()

	if flags.version {
		fmt.Printf("%s\n", version.String)
		return
	}

	if flags.oem == "" {
		fmt.Fprint(os.Stderr, "'--oem' must be provided\n")
		os.Exit(2)
	}

	if flags.stage == "" {
		fmt.Fprint(os.Stderr, "'--stage' must be provided\n")
		os.Exit(2)
	}

	for k, v := range oem.MustGet(flags.oem.String()).Flags() {
		if err := flag.Set(k, v); err != nil {
			panic(err)
		}
	}

	logger := log.New()
	defer logger.Close()

	logger.Info(version.String)

	if flags.clearCache {
		if err := os.Remove(flags.configCache); err != nil {
			logger.Err("unable to clear cache: %v", err)
		}
	}

	oemConfig := oem.MustGet(flags.oem.String())
	engine := exec.Engine{
		Root:              flags.root,
		OnlineTimeout:     flags.onlineTimeout,
		Logger:            &logger,
		ConfigCache:       flags.configCache,
		Provider:          oemConfig.Provider().Create(&logger),
		OemBaseConfig:     oemConfig.BaseConfig(),
		DefaultUserConfig: oemConfig.DefaultUserConfig(),
	}

	if !engine.Run(flags.stage.String()) {
		os.Exit(1)
	}
}