func TestNewLoggingServeMux(t *testing.T) {
	server := GetLoggingHTTPServer(t, "test")

	go server.Start()

	hook := test.NewGlobal()

	_, err := http.Get("http://localhost:8888/")
	if err != nil {
		t.Fatalf("Unexpected error: %v", err)
	}

	if len(hook.Entries) != 1 {
		t.Fatalf("Must return 1 but : %d", len(hook.Entries))
	}

	go server.Stop()
	<-server.Done
}
示例#2
0
文件: run_test.go 项目: NetSys/quilt
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)
		}
	}
}