Gitfed
bastien-mrq/gitfed / cmd / gitfed-install / podstatus_test.go
package main

import "testing"

func TestParsePodStatusReady(t *testing.T) {
	out := "NAME                      READY   STATUS    RESTARTS   AGE\n" +
		"gitfed-55b785dc9c-sljxk   2/2     Running   0          45s\n"
	ps, ok := parsePodStatus(out)
	if !ok {
		t.Fatal("expected a pod line to be found")
	}
	if ps.Phase != podReady {
		t.Errorf("phase = %v, want podReady", ps.Phase)
	}
	if ps.ReadyContainers != 2 || ps.TotalContainers != 2 {
		t.Errorf("ready/total = %d/%d, want 2/2", ps.ReadyContainers, ps.TotalContainers)
	}
}

func TestParsePodStatusErrImageNeverPull(t *testing.T) {
	// The exact shape from the real screenshot this feature was built to
	// diagnose (see the CHANGELOG/INSTALL.md fix this session).
	out := "NAME                       READY   STATUS               RESTARTS   AGE\n" +
		"cm-acme-http-solver-wxldr  1/1     Running              0          25s\n" +
		"gitfed-55b785dc9c-sljxk    0/2     ErrImageNeverPull    0          29s\n"
	ps, ok := parsePodStatus(out)
	if !ok {
		t.Fatal("expected a pod line to be found")
	}
	if ps.Phase != podErrImageNeverPull {
		t.Errorf("phase = %v, want podErrImageNeverPull", ps.Phase)
	}
}

func TestParsePodStatusNoPodYet(t *testing.T) {
	_, ok := parsePodStatus("NAME   READY   STATUS   RESTARTS   AGE\n")
	if ok {
		t.Error("expected ok=false when no gitfed- pod line is present")
	}
}

func TestDiagnoseImageTagMismatch(t *testing.T) {
	deployment := "          image: gitfed:1.2.4\n          imagePullPolicy: Never\n"
	ctrImages := "REF                             TYPE    DIGEST\n" +
		"docker.io/library/gitfed:latest linux/amd64 sha256:abc\n"

	d := diagnoseImageTag(deployment, ctrImages)
	if d.Expected != "gitfed:1.2.4" {
		t.Errorf("Expected = %q, want gitfed:1.2.4", d.Expected)
	}
	if d.Matches {
		t.Error("expected Matches=false: deployment wants 1.2.4, only :latest was imported")
	}
	if len(d.Imported) != 1 || d.Imported[0] != "gitfed:latest" {
		t.Errorf("Imported = %v, want [gitfed:latest]", d.Imported)
	}
}

func TestDiagnoseImageTagMatch(t *testing.T) {
	deployment := "          image: gitfed:1.2.4\n"
	ctrImages := "docker.io/library/gitfed:1.2.4 linux/amd64 sha256:abc\n"

	d := diagnoseImageTag(deployment, ctrImages)
	if !d.Matches {
		t.Errorf("expected Matches=true, got Expected=%q Imported=%v", d.Expected, d.Imported)
	}
}