Example #1
0
func TestPromptsUser(t *testing.T) {
	oldConfirm := confirm
	defer func() {
		confirm = oldConfirm
	}()

	util.AppFs = afero.NewMemMapFs()
	for _, confirmResp := range []bool{true, false} {
		confirm = func(in io.Reader, prompt string) (bool, error) {
			return confirmResp, nil
		}

		mockGetter := new(testutils.Getter)
		c := &clientMock.Client{
			ClusterReturn: []db.Cluster{
				{
					Spec: `{"old":"spec"}`,
				},
			},
		}
		mockGetter.On("Client", mock.Anything).Return(c, nil)

		util.WriteFile("test.js", []byte(""), 0644)
		runCmd := NewRunCommand()
		runCmd.clientGetter = mockGetter
		runCmd.stitch = "test.js"
		runCmd.Run()
		assert.Equal(t, confirmResp, c.DeployArg != "")
	}
}
Example #2
0
func TestStopNamespace(t *testing.T) {
	t.Parallel()

	mockGetter := new(testutils.Getter)
	c := &clientMock.Client{}
	mockGetter.On("Client", mock.Anything).Return(c, nil)

	stopCmd := NewStopCommand()
	stopCmd.clientGetter = mockGetter
	stopCmd.namespace = "namespace"
	stopCmd.Run()
	expStitch := `{"namespace": "namespace"}`
	if c.DeployArg != expStitch {
		t.Error("stop command invoked Quilt with the wrong stitch")
	}
}
Example #3
0
func TestExecPTY(t *testing.T) {
	isTerminal = func() bool {
		return true
	}
	workerHost := "worker"
	targetContainer := 1

	mockGetter := new(testutils.Getter)
	mockGetter.On("Client", mock.Anything).Return(&clientMock.Client{}, nil)
	mockGetter.On("ContainerClient", mock.Anything, mock.Anything).Return(
		&clientMock.Client{
			ContainerReturn: []db.Container{
				{
					StitchID: targetContainer,
					DockerID: "foo",
				},
			},
			HostReturn: workerHost,
		}, nil)

	mockSSHClient := new(testutils.MockSSHClient)
	execCmd := Exec{
		privateKey:      "key",
		command:         "cat /etc/hosts",
		allocatePTY:     true,
		targetContainer: targetContainer,
		SSHClient:       mockSSHClient,
		clientGetter:    mockGetter,
		common: &commonFlags{
			host: api.DefaultSocket,
		},
	}

	mockSSHClient.On("Connect", workerHost, "key").Return(nil)
	mockSSHClient.On("RequestPTY").Return(nil)
	mockSSHClient.On("Run", "docker exec -it foo cat /etc/hosts").Return(nil)
	mockSSHClient.On("Disconnect").Return(nil)

	execCmd.Run()

	mockSSHClient.AssertExpectations(t)
}
Example #4
0
func TestLog(t *testing.T) {
	t.Parallel()

	workerHost := "worker"
	targetContainer := 1

	mockGetter := new(testutils.Getter)
	mockGetter.On("Client", mock.Anything).Return(&clientMock.Client{}, nil)
	mockGetter.On("ContainerClient", mock.Anything, mock.Anything).Return(
		&clientMock.Client{
			ContainerReturn: []db.Container{
				{
					StitchID: targetContainer,
					DockerID: "foo",
				},
			},
			HostReturn: workerHost,
		}, nil)
	mockSSHClient := new(testutils.MockSSHClient)
	logsCmd := Log{
		privateKey:      "key",
		targetContainer: targetContainer,
		shouldTail:      true,
		showTimestamps:  true,
		sinceTimestamp:  "2006-01-02T15:04:05",
		SSHClient:       mockSSHClient,
		clientGetter:    mockGetter,
		common: &commonFlags{
			host: api.DefaultSocket,
		},
	}

	mockSSHClient.On("Connect", workerHost, "key").Return(nil)
	mockSSHClient.On("Run", "docker logs --since=2006-01-02T15:04:05 --timestamps "+
		"--follow foo").Return(nil)
	mockSSHClient.On("Disconnect").Return(nil)

	logsCmd.Run()

	mockSSHClient.AssertExpectations(t)
}
Example #5
0
func TestRunSpec(t *testing.T) {
	os.Setenv("QUILT_PATH", "/quilt_path")
	stitch.DefaultImportGetter.Path = "/quilt_path"

	exJavascript := `deployment.deploy(new Machine({}));`
	exJSON := `{"Containers":[],"Labels":[],"Connections":[],"Placements":[],` +
		`"Machines":[{"Provider":"","Role":"","Size":"",` +
		`"CPU":{"Min":0,"Max":0},"RAM":{"Min":0,"Max":0},"DiskSize":0,` +
		`"Region":"","SSHKeys":[]}],"AdminACL":[],"MaxPrice":0,` +
		`"Namespace":"default-namespace","Invariants":[]}`
	tests := []runTest{
		{
			files: []file{
				{
					path:     "test.js",
					contents: exJavascript,
				},
			},
			path:         "test.js",
			expExitCode:  0,
			expDeployArg: exJSON,
		},
		{
			path:        "dne.js",
			expExitCode: 1,
			expEntries: []log.Entry{
				{
					Message: "open /quilt_path/dne.js: " +
						"file does not exist",
					Level: log.ErrorLevel,
				},
			},
		},
		{
			path:        "/dne.js",
			expExitCode: 1,
			expEntries: []log.Entry{
				{
					Message: "open /dne.js: file does not exist",
					Level:   log.ErrorLevel,
				},
			},
		},
		{
			files: []file{
				{
					path:     "/quilt_path/in_quilt_path.js",
					contents: exJavascript,
				},
			},
			path:         "in_quilt_path",
			expDeployArg: exJSON,
		},
		// Ensure we print a stacktrace when available.
		{
			files: []file{
				{
					path:     "/quilt_path/A.js",
					contents: `require("B").foo();`,
				},
				{
					path: "/quilt_path/B.js",
					contents: `module.exports.foo = function() {
						throw new Error("bar");
					}`,
				},
			},
			path:        "/quilt_path/A.js",
			expExitCode: 1,
			expEntries: []log.Entry{
				{
					Message: "Error: bar\n" +
						"    at /quilt_path/B.js:2:17\n" +
						"    at /quilt_path/A.js:1:67\n",
					Level: log.ErrorLevel,
				},
			},
		},
	}
	for _, test := range tests {
		util.AppFs = afero.NewMemMapFs()

		mockGetter := new(testutils.Getter)
		c := &clientMock.Client{}
		mockGetter.On("Client", mock.Anything).Return(c, nil)

		logHook := logrusTestHook.NewGlobal()

		for _, f := range test.files {
			util.WriteFile(f.path, []byte(f.contents), 0644)
		}
		runCmd := NewRunCommand()
		runCmd.clientGetter = mockGetter
		runCmd.stitch = test.path
		exitCode := runCmd.Run()

		assert.Equal(t, test.expExitCode, exitCode)
		assert.Equal(t, test.expDeployArg, c.DeployArg)

		assert.Equal(t, len(test.expEntries), len(logHook.Entries))
		for i, entry := range logHook.Entries {
			assert.Equal(t, test.expEntries[i].Message, entry.Message)
			assert.Equal(t, test.expEntries[i].Level, entry.Level)
		}
	}
}