Example #1
0
File: main.go Project: deoxxa/mllp
func main() {
	rx := regexp.MustCompile(os.Args[1])

	r := mllp.NewReader(os.Stdin)
	w := mllp.NewWriter(os.Stdout)

	i := 0
	for {
		m, err := r.ReadMessage()
		if err != nil {
			if stackerr.HasUnderlying(err, stackerr.Equals(io.EOF)) {
				break
			}

			panic(err)
		}

		if rx.Match(m) {
			if err := w.WriteMessage(m); err != nil {
				panic(err)
			}
		}

		i++
	}
}
func TestApplyWithExistingRemoveError(t *testing.T) {
	const image = "x"
	givenErr := errors.New("")
	container := &Container{
		containerConfig: &dockerclient.ContainerConfig{
			Image: image,
		},
		removeExisting: true,
	}
	client := &mockClient{
		inspectContainer: func(name string) (*dockerclient.ContainerInfo, error) {
			return &dockerclient.ContainerInfo{
				Id:    "y",
				Image: "z",
			}, nil
		},
		listImages: func() ([]*dockerclient.Image, error) {
			return []*dockerclient.Image{
				{
					RepoTags: []string{image},
					Id:       "y",
				},
			}, nil
		},
		removeContainer: func(id string, force, volumes bool) error {
			return givenErr
		},
	}
	err := container.Apply(client)
	ensure.True(t, stackerr.HasUnderlying(err, stackerr.Equals(givenErr)))
}
func TestApplyStartError(t *testing.T) {
	givenErr := errors.New("")
	const image = "x"
	const id = "y"
	container := &Container{
		containerConfig: &dockerclient.ContainerConfig{
			Image: image,
		},
	}
	client := &mockClient{
		inspectContainer: func(name string) (*dockerclient.ContainerInfo, error) {
			return &dockerclient.ContainerInfo{
				Id:     "a",
				Image:  id,
				Config: &dockerclient.ContainerConfig{},
			}, nil
		},
		listImages: func() ([]*dockerclient.Image, error) {
			return []*dockerclient.Image{
				{
					RepoTags: []string{image},
					Id:       id,
				},
			}, nil
		},
		startContainer: func(id string, config *dockerclient.HostConfig) error {
			return givenErr
		},
	}
	err := container.Apply(client)
	ensure.True(t, stackerr.HasUnderlying(err, stackerr.Equals(givenErr)))
}
func TestApplyInitialInspectError(t *testing.T) {
	givenErr := errors.New("")
	container := &Container{
		containerConfig: &dockerclient.ContainerConfig{
			Image: "x",
		},
	}
	client := &mockClient{
		inspectContainer: func(name string) (*dockerclient.ContainerInfo, error) {
			return nil, givenErr
		},
	}
	err := container.Apply(client)
	ensure.True(t, stackerr.HasUnderlying(err, stackerr.Equals(givenErr)))
}
func TestApplyGraphApplyError(t *testing.T) {
	givenErr := errors.New("")
	containers := []*Container{
		{
			name:            "n1",
			containerConfig: &dockerclient.ContainerConfig{Image: "in1"},
		},
	}
	client := &mockClient{
		inspectContainer: func(name string) (*dockerclient.ContainerInfo, error) {
			return nil, givenErr
		},
	}
	err := ApplyGraph(client, containers)
	ensure.True(t, stackerr.HasUnderlying(err, stackerr.Equals(givenErr)))
}
func TestCheckExistingImageIdentifyError(t *testing.T) {
	givenErr := errors.New("")
	const image = "x"
	container := &Container{
		containerConfig: &dockerclient.ContainerConfig{
			Image: image,
		},
	}
	client := &mockClient{
		listImages: func() ([]*dockerclient.Image, error) {
			return nil, givenErr
		},
	}
	ok, err := container.checkExisting(client, &dockerclient.ContainerInfo{Image: "z"})
	ensure.True(t, stackerr.HasUnderlying(err, stackerr.Equals(givenErr)))
	ensure.False(t, ok)
}
func TestApplyInspectAfterCreateError(t *testing.T) {
	container, err := NewContainer(
		ContainerName("x"),
		ContainerConfig(&dockerclient.ContainerConfig{Image: "foo"}),
	)
	ensure.Nil(t, err)
	client := &mockClient{
		inspectContainer: func(name string) (*dockerclient.ContainerInfo, error) {
			return nil, dockerclient.ErrNotFound
		},
		createContainer: func(config *dockerclient.ContainerConfig, name string) (string, error) {
			return "baz", nil
		},
	}
	err = container.Apply(client)
	ensure.True(t, stackerr.HasUnderlying(err, stackerr.Equals(dockerclient.ErrNotFound)))
}
Example #8
0
File: main.go Project: deoxxa/mllp
func main() {
	r := mllp.NewReader(os.Stdin)

	i := 0
	for {
		if _, err := r.ReadMessage(); err != nil {
			if stackerr.HasUnderlying(err, stackerr.Equals(io.EOF)) {
				break
			}

			panic(err)
		}

		i++
	}

	fmt.Printf("total messages: %d\n", i)
}
func TestApplyRemovesExistingWithoutDesiredImageError(t *testing.T) {
	givenErr := errors.New("")
	container := &Container{
		containerConfig: &dockerclient.ContainerConfig{
			Image: "x",
		},
	}
	client := &mockClient{
		inspectContainer: func(name string) (*dockerclient.ContainerInfo, error) {
			return &dockerclient.ContainerInfo{Id: "y", Image: "a"}, nil
		},
		listImages: func() ([]*dockerclient.Image, error) {
			return nil, givenErr
		},
	}
	err := container.Apply(client)
	ensure.True(t, stackerr.HasUnderlying(err, stackerr.Equals(givenErr)))
}
func TestApplyForceRemoveExistingError(t *testing.T) {
	container, err := NewContainer(
		ContainerName("x"),
		ContainerConfig(&dockerclient.ContainerConfig{Image: "foo"}),
		ContainerForceRemoveExisting(),
	)
	ensure.Nil(t, err)
	givenErr := errors.New("")
	client := &mockClient{
		removeContainer: func(id string, force, volumes bool) error {
			return givenErr
		},
		inspectContainer: func(name string) (*dockerclient.ContainerInfo, error) {
			return &dockerclient.ContainerInfo{Id: "x"}, nil
		},
	}
	err = container.Apply(client)
	ensure.True(t, stackerr.HasUnderlying(err, stackerr.Equals(givenErr)))
}
func TestApplyAfterCreateError(t *testing.T) {
	givenErr := errors.New("")
	const givenName = "x"
	const givenID = "y"
	var inspectCalls, removeCalls int
	container, err := NewContainer(
		ContainerName(givenName),
		ContainerConfig(&dockerclient.ContainerConfig{Image: "foo"}),
		ContainerAfterCreate(func(string) error { return givenErr }),
	)
	ensure.Nil(t, err)
	client := &mockClient{
		inspectContainer: func(name string) (*dockerclient.ContainerInfo, error) {
			inspectCalls++
			switch inspectCalls {
			case 1:
				return nil, dockerclient.ErrNotFound
			case 2:
				return &dockerclient.ContainerInfo{Id: givenID}, nil
			}
			panic("not reached")
		},
		createContainer: func(config *dockerclient.ContainerConfig, name string) (string, error) {
			return "", nil
		},
		startContainer: func(id string, config *dockerclient.HostConfig) error {
			return nil
		},
		removeContainer: func(id string, force, volumes bool) error {
			removeCalls++
			ensure.DeepEqual(t, id, givenID)
			return nil
		},
	}
	err = container.Apply(client)
	ensure.True(t, stackerr.HasUnderlying(err, stackerr.Equals(givenErr)))
	ensure.DeepEqual(t, removeCalls, 1)
}