Example #1
0
func TestExpose(t *testing.T) {
	oldExecCmd := exec.Command
	defer func() { exec.Command = oldExecCmd }()

	psCalled := false
	exec.Command = func(name string, args ...string) exec.Cmd {
		if args[0] == "expose" {
			t.Fatal("Expose not expected")
			return nil
		}
		psCalled = true
		return testExec.NewMockCmdString(fmt.Sprintf("%s %s %s/24\n", mockContainerID, mockContainerMAC, mockContainerIP))

	}

	client := weave.NewClient("")
	if err := client.Expose(); err != nil {
		t.Fatal(err)
	}

	if !psCalled {
		t.Fatal("Expected a call to weave ps")
	}

	psCalled, exposeCalled := false, false
	exec.Command = func(name string, args ...string) exec.Cmd {
		if args[0] == "expose" {
			exposeCalled = true
			return testExec.NewMockCmdString("")
		}
		psCalled = true
		return testExec.NewMockCmdString("")
	}

	if err := client.Expose(); err != nil {
		t.Fatal(err)
	}

	if !psCalled || !exposeCalled {
		t.Fatal("Expected a call to weave ps & expose")
	}
}
Example #2
0
func TestPS(t *testing.T) {
	oldExecCmd := exec.Command
	defer func() { exec.Command = oldExecCmd }()
	exec.Command = func(name string, args ...string) exec.Cmd {
		return testExec.NewMockCmdString(fmt.Sprintf("%s %s %s/24\n", mockContainerID, mockContainerMAC, mockContainerIP))
	}

	client := weave.NewClient("")
	entries, err := client.PS()
	if err != nil {
		t.Fatal(err)
	}

	want := map[string]weave.PSEntry{
		mockContainerID: {
			ContainerIDPrefix: mockContainerID,
			MACAddress:        mockContainerMAC,
			IPs:               []string{mockContainerIP},
		},
	}
	if !reflect.DeepEqual(entries, want) {
		t.Fatal(test.Diff(entries, want))
	}
}