Gitfed
bastien-mrq/gitfed / cmd / gitfed-ctl / update_pipeline_test.go
package main

import (
	"os"
	"path/filepath"
	"strings"
	"testing"
)

func TestBumpDeploymentImageTag(t *testing.T) {
	dir := t.TempDir()
	if err := os.MkdirAll(filepath.Join(dir, "deploy", "k8s"), 0755); err != nil {
		t.Fatal(err)
	}
	original := `apiVersion: apps/v1
kind: Deployment
spec:
  template:
    spec:
      containers:
        - name: server
          image: gitfed:1.2.7
        - name: web
          image: gitfed:1.2.7
`
	path := filepath.Join(dir, "deploy", "k8s", "deployment.yaml")
	if err := os.WriteFile(path, []byte(original), 0644); err != nil {
		t.Fatal(err)
	}

	if err := bumpDeploymentImageTag(dir, "1.2.10"); err != nil {
		t.Fatalf("bumpDeploymentImageTag: %v", err)
	}

	updated, err := os.ReadFile(path)
	if err != nil {
		t.Fatal(err)
	}
	got := string(updated)
	if strings.Count(got, "image: gitfed:1.2.10") != 2 {
		t.Errorf("expected both image lines rewritten to 1.2.10, got:\n%s", got)
	}
	if strings.Contains(got, "1.2.7") {
		t.Errorf("expected no trace of the old tag left, got:\n%s", got)
	}
	// non-image lines must be untouched
	if !strings.Contains(got, "name: server") || !strings.Contains(got, "name: web") {
		t.Errorf("unrelated lines were modified:\n%s", got)
	}
}