Ejemplo n.º 1
0
func TestContext_NumFlags(t *testing.T) {
	set := flag.NewFlagSet("test", 0)
	set.Bool("myflag", false, "doc")
	set.String("otherflag", "hello world", "doc")
	globalSet := flag.NewFlagSet("test", 0)
	globalSet.Bool("myflagGlobal", true, "doc")
	globalCtx := cli.NewContext(nil, globalSet, nil)
	c := cli.NewContext(nil, set, globalCtx)
	set.Parse([]string{"--myflag", "--otherflag=foo"})
	globalSet.Parse([]string{"--myflagGlobal"})
	expect(t, c.NumFlags(), 2)
}
Ejemplo n.º 2
0
func TestNewContext(t *testing.T) {
	set := flag.NewFlagSet("test", 0)
	set.Int("myflag", 12, "doc")
	globalSet := flag.NewFlagSet("test", 0)
	globalSet.Int("myflag", 42, "doc")
	globalCtx := cli.NewContext(nil, globalSet, nil)
	command := cli.Command{Name: "mycommand"}
	c := cli.NewContext(nil, set, globalCtx)
	c.Command = command
	expect(t, c.Int("myflag"), 12)
	expect(t, c.GlobalInt("myflag"), 42)
	expect(t, c.Command.Name, "mycommand")
}
Ejemplo n.º 3
0
func TestContext_IsSet(t *testing.T) {
	set := flag.NewFlagSet("test", 0)
	set.Bool("myflag", false, "doc")
	set.String("otherflag", "hello world", "doc")
	globalSet := flag.NewFlagSet("test", 0)
	globalSet.Bool("myflagGlobal", true, "doc")
	globalCtx := cli.NewContext(nil, globalSet, nil)
	c := cli.NewContext(nil, set, globalCtx)
	set.Parse([]string{"--myflag", "bat", "baz"})
	globalSet.Parse([]string{"--myflagGlobal", "bat", "baz"})
	expect(t, c.IsSet("myflag"), true)
	expect(t, c.IsSet("otherflag"), false)
	expect(t, c.IsSet("bogusflag"), false)
	expect(t, c.IsSet("myflagGlobal"), false)
}
Ejemplo n.º 4
0
func TestUploadHandleFlags(t *testing.T) {
	app := cli.NewApp()
	flagset := flag.NewFlagSet("flags", 1)
	flagset.String("name", "", "")
	flagset.String("file", "", "")
	flagset.String("public-key", "", "")
	flagset.Set("name", "keypair1Name")
	flagset.Set("public-key", "ssh public key data here")
	c := cli.NewContext(app, flagset, nil)
	cmd := &commandUpload{
		Ctx: &handler.Context{
			CLIContext: c,
		},
	}
	expected := &handler.Resource{
		Params: &paramsUpload{
			opts: &osKeypairs.CreateOpts{
				Name:      "keypair1Name",
				PublicKey: "ssh public key data here",
			},
		},
	}
	actual := &handler.Resource{}
	err := cmd.HandleFlags(actual)
	th.AssertNoErr(t, err)
	th.AssertDeepEquals(t, *expected.Params.(*paramsUpload).opts, *actual.Params.(*paramsUpload).opts)
}
Ejemplo n.º 5
0
func TestCreateHandleFlags(t *testing.T) {
	app := cli.NewApp()
	flagset := flag.NewFlagSet("flags", 1)
	flagset.String("metadata", "", "")
	flagset.String("container-read", "", "")
	flagset.String("container-write", "", "")
	flagset.Set("metadata", "key=val,foo=bar")
	c := cli.NewContext(app, flagset, nil)
	cmd := &commandCreate{
		Ctx: &handler.Context{
			CLIContext: c,
		},
	}
	expected := &handler.Resource{
		Params: &paramsCreate{
			opts: containers.CreateOpts{
				Metadata: map[string]string{
					"key": "val",
					"foo": "bar",
				},
			},
		},
	}
	actual := &handler.Resource{}
	err := cmd.HandleFlags(actual)
	th.AssertNoErr(t, err)
	th.AssertDeepEquals(t, expected.Params.(*paramsCreate).opts, actual.Params.(*paramsCreate).opts)
}
Ejemplo n.º 6
0
func TestDeleteExecute(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()
	th.Mux.HandleFunc("/container1", func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusNoContent)
	})
	app := cli.NewApp()
	flagset := flag.NewFlagSet("flags", 1)
	flagset.String("name", "", "")
	flagset.Set("name", "container1")
	c := cli.NewContext(app, flagset, nil)
	cmd := &commandDelete{
		Ctx: &handler.Context{
			ServiceClient: client.ServiceClient(),
			CLIContext:    c,
		},
	}
	actual := &handler.Resource{
		Params: &paramsDelete{
			container: "container1",
		},
	}
	cmd.Execute(actual)
	th.AssertNoErr(t, actual.Err)
}
Ejemplo n.º 7
0
func TestListHandleFlags(t *testing.T) {
	app := cli.NewApp()
	flagset := flag.NewFlagSet("flags", 1)
	flagset.String("image", "", "")
	flagset.String("flavor", "", "")
	flagset.String("name", "", "")
	flagset.String("status", "", "")
	flagset.String("marker", "", "")
	flagset.Set("image", "13ba-75c0-4483-acf9")
	flagset.Set("flavor", "1234-b95f-ac5b-cd23")
	flagset.Set("name", "server*")
	flagset.Set("status", "AVAILABLE")
	flagset.Set("marker", "1fd3-4f9f-44df-1b5c")
	c := cli.NewContext(app, flagset, nil)
	cmd := &commandList{
		Ctx: &handler.Context{
			CLIContext: c,
		},
	}
	expected := &handler.Resource{
		Params: &paramsList{
			opts: &osServers.ListOpts{
				Image:  "13ba-75c0-4483-acf9",
				Flavor: "1234-b95f-ac5b-cd23",
				Name:   "server*",
				Status: "AVAILABLE",
				Marker: "1fd3-4f9f-44df-1b5c",
			},
		},
	}
	actual := &handler.Resource{}
	err := cmd.HandleFlags(actual)
	th.AssertNoErr(t, err)
	th.AssertDeepEquals(t, *expected.Params.(*paramsList).opts, *actual.Params.(*paramsList).opts)
}
Ejemplo n.º 8
0
func TestGenerateExecute(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()
	th.Mux.HandleFunc("/os-keypairs", func(w http.ResponseWriter, r *http.Request) {
		w.Header().Add("Content-Type", "application/json")
		fmt.Fprintf(w, `{"keypair":{}}`)
	})
	app := cli.NewApp()
	flagset := flag.NewFlagSet("flags", 1)
	flagset.Bool("json", false, "")
	c := cli.NewContext(app, flagset, nil)
	cmd := &commandGenerate{
		Ctx: &handler.Context{
			CLIContext:    c,
			ServiceClient: client.ServiceClient(),
		},
	}
	actual := &handler.Resource{
		Params: &paramsGenerate{
			opts: &osKeypairs.CreateOpts{
				Name: "keypair1Name",
			},
		},
	}
	cmd.Execute(actual)
	th.AssertNoErr(t, actual.Err)
}
Ejemplo n.º 9
0
func TestGetHandleSingle(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()
	th.Mux.HandleFunc("/servers/detail", func(w http.ResponseWriter, r *http.Request) {
		w.Header().Add("Content-Type", "application/json")
		fmt.Fprintf(w, `{"servers":[{"ID":"server1","Name":"server1Name"}]}`)
	})
	app := cli.NewApp()
	flagset := flag.NewFlagSet("flags", 1)
	flagset.String("name", "", "")
	flagset.Set("name", "server1Name")
	c := cli.NewContext(app, flagset, nil)
	cmd := &commandGet{
		Ctx: &handler.Context{
			CLIContext:    c,
			ServiceClient: client.ServiceClient(),
		},
	}
	expected := &handler.Resource{
		Params: &paramsGet{
			server: "server1",
		},
	}
	actual := &handler.Resource{
		Params: &paramsGet{},
	}
	err := cmd.HandleSingle(actual)
	th.AssertNoErr(t, err)
	th.AssertEquals(t, expected.Params.(*paramsGet).server, actual.Params.(*paramsGet).server)
}
Ejemplo n.º 10
0
func TestGenerateHandleSingle(t *testing.T) {
	app := cli.NewApp()
	flagset := flag.NewFlagSet("flags", 1)
	flagset.String("name", "", "")
	flagset.Set("name", "keypair1Name")
	c := cli.NewContext(app, flagset, nil)
	cmd := &commandGenerate{
		Ctx: &handler.Context{
			CLIContext: c,
		},
	}
	expected := &handler.Resource{
		Params: &paramsGenerate{
			opts: &osKeypairs.CreateOpts{
				Name: "keypair1Name",
			},
		},
	}
	actual := &handler.Resource{
		Params: &paramsGenerate{
			opts: &osKeypairs.CreateOpts{},
		},
	}
	err := cmd.HandleSingle(actual)
	th.AssertNoErr(t, err)
	th.AssertEquals(t, *expected.Params.(*paramsGenerate).opts, *actual.Params.(*paramsGenerate).opts)
}
Ejemplo n.º 11
0
func TestContext_Args(t *testing.T) {
	set := flag.NewFlagSet("test", 0)
	set.Bool("myflag", false, "doc")
	c := cli.NewContext(nil, set, nil)
	set.Parse([]string{"--myflag", "bat", "baz"})
	expect(t, len(c.Args()), 2)
	expect(t, c.Bool("myflag"), true)
}
Ejemplo n.º 12
0
func TestDeleteContext(t *testing.T) {
	app := cli.NewApp()
	flagset := flag.NewFlagSet("flags", 1)
	c := cli.NewContext(app, flagset, nil)
	cmd := &commandDelete{
		Ctx: &handler.Context{
			CLIContext: c,
		},
	}
	expected := cmd.Ctx
	actual := cmd.Context()
	th.AssertDeepEquals(t, expected, actual)
}
Ejemplo n.º 13
0
func Test_ShowAppHelp_NoAuthor(t *testing.T) {
	output := new(bytes.Buffer)
	app := cli.NewApp()
	app.Writer = output

	c := cli.NewContext(app, nil, nil)

	cli.ShowAppHelp(c)

	if bytes.Index(output.Bytes(), []byte("AUTHOR(S):")) != -1 {
		t.Errorf("expected\n%snot to include %s", output.String(), "AUTHOR(S):")
	}
}
Ejemplo n.º 14
0
func TestCreateHandleFlags(t *testing.T) {
	app := cli.NewApp()
	flagset := flag.NewFlagSet("flags", 1)
	flagset.String("image-id", "", "")
	flagset.String("flavor-id", "", "")
	flagset.String("security-groups", "", "")
	flagset.String("networks", "", "")
	flagset.String("metadata", "", "")
	flagset.String("admin-pass", "", "")
	flagset.String("keypair", "", "")
	flagset.Set("image-id", "13ba-75c0-4483-acf9")
	flagset.Set("flavor-id", "1234-b95f-ac5b-cd23")
	flagset.Set("security-groups", "sg1,sg2,sg3")
	flagset.Set("networks", "1111-2222-3333-4444,5555-7777-8888-9999")
	flagset.Set("metadata", "img=foo,flavor=bar")
	flagset.Set("admin-pass", "secret")
	flagset.Set("keypair", "kp1")
	c := cli.NewContext(app, flagset, nil)
	cmd := &commandCreate{
		Ctx: &handler.Context{
			CLIContext: c,
		},
	}
	expected := &handler.Resource{
		Params: &paramsCreate{
			opts: &servers.CreateOpts{
				ImageRef:       "13ba-75c0-4483-acf9",
				FlavorRef:      "1234-b95f-ac5b-cd23",
				SecurityGroups: []string{"sg1", "sg2", "sg3"},
				Networks: []osServers.Network{
					{
						UUID: "1111-2222-3333-4444",
					},
					{
						UUID: "5555-7777-8888-9999",
					},
				},
				Metadata: map[string]string{
					"img":    "foo",
					"flavor": "bar",
				},
				AdminPass: "******",
				KeyPair:   "kp1",
			},
		},
	}
	actual := &handler.Resource{}
	err := cmd.HandleFlags(actual)
	th.AssertNoErr(t, err)
	th.AssertDeepEquals(t, *expected.Params.(*paramsCreate).opts, *actual.Params.(*paramsCreate).opts)
}
Ejemplo n.º 15
0
func TestRebuildHandleFlags(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()
	th.Mux.HandleFunc("/servers/detail", func(w http.ResponseWriter, r *http.Request) {
		w.Header().Add("Content-Type", "application/json")
		fmt.Fprintf(w, `{"servers":[{"ID":"server1","Name":"server1Name"}]}`)
	})

	app := cli.NewApp()
	flagset := flag.NewFlagSet("flags", 1)
	flagset.String("id", "", "")
	flagset.String("image-id", "", "")
	flagset.String("admin-pass", "", "")
	flagset.String("ipv4", "", "")
	flagset.String("ipv6", "", "")
	flagset.String("metadata", "", "")
	flagset.String("rename", "", "")
	flagset.Set("id", "server1")
	flagset.Set("image-id", "123456789")
	flagset.Set("admin-pass", "secret")
	flagset.Set("ipv4", "123.45.67.89")
	flagset.Set("metadata", "img=bar,flavor=foo")
	flagset.Set("rename", "server1Rename")
	c := cli.NewContext(app, flagset, nil)
	cmd := &commandRebuild{
		Ctx: &handler.Context{
			CLIContext: c,
		},
	}
	expected := &handler.Resource{
		Params: &paramsRebuild{
			opts: &servers.RebuildOpts{
				ImageID:    "123456789",
				AdminPass:  "******",
				AccessIPv4: "123.45.67.89",
				Metadata: map[string]string{
					"img":    "bar",
					"flavor": "foo",
				},
				Name: "server1Rename",
			},
			serverID: "server1",
		},
	}
	actual := &handler.Resource{}
	err := cmd.HandleFlags(actual)
	th.AssertNoErr(t, err)
	th.AssertDeepEquals(t, *expected.Params.(*paramsRebuild).opts, *actual.Params.(*paramsRebuild).opts)
}
Ejemplo n.º 16
0
func TestDeleteHandleFlags(t *testing.T) {
	app := cli.NewApp()
	flagset := flag.NewFlagSet("flags", 1)
	c := cli.NewContext(app, flagset, nil)
	cmd := &commandDelete{
		Ctx: &handler.Context{
			CLIContext: c,
		},
	}
	expected := &handler.Resource{
		Params: &paramsDelete{},
	}
	actual := &handler.Resource{}
	err := cmd.HandleFlags(actual)
	th.AssertNoErr(t, err)
	th.AssertDeepEquals(t, expected.Params.(*paramsDelete), actual.Params.(*paramsDelete))
}
Ejemplo n.º 17
0
func TestCommandDoNotIgnoreFlags(t *testing.T) {
	app := cli.NewApp()
	set := flag.NewFlagSet("test", 0)
	test := []string{"blah", "blah", "-break"}
	set.Parse(test)

	c := cli.NewContext(app, set, nil)

	command := cli.Command{
		Name:        "test-cmd",
		Aliases:     []string{"tc"},
		Usage:       "this is for testing",
		Description: "testing",
		Action:      func(_ *cli.Context) {},
	}
	err := command.Run(c)

	expect(t, err.Error(), "flag provided but not defined: -break")
}
Ejemplo n.º 18
0
func TestAppVersionPrinter(t *testing.T) {
	oldPrinter := cli.VersionPrinter
	defer func() {
		cli.VersionPrinter = oldPrinter
	}()

	var wasCalled = false
	cli.VersionPrinter = func(c *cli.Context) {
		wasCalled = true
	}

	app := cli.NewApp()
	ctx := cli.NewContext(app, nil, nil)
	cli.ShowVersion(ctx)

	if wasCalled == false {
		t.Errorf("Version printer expected to be called, but was not")
	}
}
Ejemplo n.º 19
0
func TestCommandIgnoreFlags(t *testing.T) {
	app := cli.NewApp()
	set := flag.NewFlagSet("test", 0)
	test := []string{"blah", "blah"}
	set.Parse(test)

	c := cli.NewContext(app, set, nil)

	command := cli.Command{
		Name:            "test-cmd",
		Aliases:         []string{"tc"},
		Usage:           "this is for testing",
		Description:     "testing",
		Action:          func(_ *cli.Context) {},
		SkipFlagParsing: true,
	}
	err := command.Run(c)

	expect(t, err, nil)
}
Ejemplo n.º 20
0
func TestRebootHandleFlags(t *testing.T) {
	app := cli.NewApp()
	flagset := flag.NewFlagSet("flags", 1)
	flagset.Bool("soft", false, "")
	flagset.Bool("hard", false, "")
	flagset.Set("soft", "true")
	c := cli.NewContext(app, flagset, nil)
	cmd := &commandReboot{
		Ctx: &handler.Context{
			CLIContext: c,
		},
	}
	expected := &handler.Resource{
		Params: &paramsReboot{
			how: osServers.OSReboot,
		},
	}
	actual := &handler.Resource{}
	err := cmd.HandleFlags(actual)
	th.AssertNoErr(t, err)
	th.AssertDeepEquals(t, *expected.Params.(*paramsReboot), *actual.Params.(*paramsReboot))
}
Ejemplo n.º 21
0
func TestDeleteHandleSingle(t *testing.T) {
	app := cli.NewApp()
	flagset := flag.NewFlagSet("flags", 1)
	flagset.String("name", "", "")
	flagset.Set("name", "keypair1Name")
	c := cli.NewContext(app, flagset, nil)
	cmd := &commandDelete{
		Ctx: &handler.Context{
			CLIContext: c,
		},
	}
	expected := &handler.Resource{
		Params: &paramsDelete{
			keypair: "keypair1Name",
		},
	}
	actual := &handler.Resource{
		Params: &paramsDelete{},
	}
	err := cmd.HandleSingle(actual)
	th.AssertNoErr(t, err)
	th.AssertEquals(t, expected.Params.(*paramsDelete).keypair, actual.Params.(*paramsDelete).keypair)
}
Ejemplo n.º 22
0
func TestResizeHandleFlags(t *testing.T) {
	app := cli.NewApp()
	flagset := flag.NewFlagSet("flags", 1)
	flagset.String("flavor-id", "", "")
	flagset.Set("flavor-id", "2")
	c := cli.NewContext(app, flagset, nil)
	cmd := &commandResize{
		Ctx: &handler.Context{
			CLIContext: c,
		},
	}
	expected := &handler.Resource{
		Params: &paramsResize{
			opts: &osServers.ResizeOpts{
				FlavorRef: "2",
			},
		},
	}
	actual := &handler.Resource{}
	err := cmd.HandleFlags(actual)
	th.AssertNoErr(t, err)
	th.AssertDeepEquals(t, *expected.Params.(*paramsResize).opts, *actual.Params.(*paramsResize).opts)
}
Ejemplo n.º 23
0
func TestUpdateHandleFlags(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()
	th.Mux.HandleFunc("/servers/detail", func(w http.ResponseWriter, r *http.Request) {
		w.Header().Add("Content-Type", "application/json")
		fmt.Fprintf(w, `{"servers":[{"ID":"server1","Name":"server1Name"}]}`)
	})

	app := cli.NewApp()
	flagset := flag.NewFlagSet("flags", 1)
	flagset.String("rename", "", "")
	flagset.String("ipv4", "", "")
	flagset.String("ipv6", "", "")
	flagset.String("id", "", "")
	flagset.Set("id", "server1")
	flagset.Set("rename", "server1NewName")
	flagset.Set("ipv4", "123.45.67.89")
	c := cli.NewContext(app, flagset, nil)
	cmd := &commandUpdate{
		Ctx: &handler.Context{
			CLIContext: c,
		},
	}
	expected := &handler.Resource{
		Params: &paramsUpdate{
			opts: &osServers.UpdateOpts{
				Name:       "server1NewName",
				AccessIPv4: "123.45.67.89",
			},
			serverID: "server1",
		},
	}
	actual := &handler.Resource{}
	err := cmd.HandleFlags(actual)
	th.AssertNoErr(t, err)
	th.AssertDeepEquals(t, *expected.Params.(*paramsUpdate).opts, *actual.Params.(*paramsUpdate).opts)
}
Ejemplo n.º 24
0
func newDlCmd(fs *flag.FlagSet) *commandDownload {
	return &commandDownload{Ctx: &handler.Context{
		CLIContext: cli.NewContext(cli.NewApp(), fs, nil),
	}}
}
Ejemplo n.º 25
0
func TestContext_Int(t *testing.T) {
	set := flag.NewFlagSet("test", 0)
	set.Int("myflag", 12, "doc")
	c := cli.NewContext(nil, set, nil)
	expect(t, c.Int("myflag"), 12)
}
Ejemplo n.º 26
0
func TestContext_Duration(t *testing.T) {
	set := flag.NewFlagSet("test", 0)
	set.Duration("myflag", time.Duration(12*time.Second), "doc")
	c := cli.NewContext(nil, set, nil)
	expect(t, c.Duration("myflag"), time.Duration(12*time.Second))
}
Ejemplo n.º 27
0
func newUpDirCmd(fs *flag.FlagSet) *commandUploadDir {
	return &commandUploadDir{Ctx: &handler.Context{
		CLIContext: cli.NewContext(cli.NewApp(), fs, nil),
	}}
}
Ejemplo n.º 28
0
func TestContext_String(t *testing.T) {
	set := flag.NewFlagSet("test", 0)
	set.String("myflag", "hello world", "doc")
	c := cli.NewContext(nil, set, nil)
	expect(t, c.String("myflag"), "hello world")
}
Ejemplo n.º 29
0
func TestContext_BoolT(t *testing.T) {
	set := flag.NewFlagSet("test", 0)
	set.Bool("myflag", true, "doc")
	c := cli.NewContext(nil, set, nil)
	expect(t, c.BoolT("myflag"), true)
}
Ejemplo n.º 30
0
func newListCmd(fs *flag.FlagSet) *commandList {
	return &commandList{Ctx: &handler.Context{
		CLIContext: cli.NewContext(cli.NewApp(), fs, nil),
	}}
}