func TestDeploymentRunnable(t *testing.T, fw *framework.Framework, manifestLocation, namespace string) {
	TestDeploymentRunnableCheck(t, fw, manifestLocation, namespace, NoCheck)
}
TestDeploymentRunnable tests whether a manifest is deployable to the specified namespace.
package utils
import (
	"fmt"
	"strings"
	"testing"
	"time"
	"github.com/IBM/portieris/test/framework"
	"github.com/stretchr/testify/assert"
	appsv1 "k8s.io/api/apps/v1"
	v1 "k8s.io/api/core/v1"
)
func buildDeployment(t *testing.T, fw *framework.Framework, manifestLocation, namespace string, expectCreateFail bool) *appsv1.Deployment {
	manifest, err := fw.LoadDeploymentManifest(manifestLocation)
	if err != nil {
		t.Fatalf("Error loading manifest: %v", err)
	}
	if manifest == nil {
		t.Fatalf("Error loading manifest: manifest is nil")
	}
	if err = fw.CreateDeployment(namespace, manifest); err != nil {
		if !expectCreateFail {
			t.Fatalf("Error creating %q deployment in %v: %v", manifest.Name, namespace, err)
		} else {
			return nil
		}
	}
	fw.WaitForDeploymentPods(manifest.Name, namespace, time.Minute)
	deployment, err := fw.GetDeployment(manifest.Name, namespace)
	if err != nil {
		t.Fatalf("Error getting %q deployment in %v: %v", manifest.Name, namespace, err)
	}
	return deployment
}
func patchDeployment(t *testing.T, fw *framework.Framework, deploymentName, namespace, patchString string, expectCreateFail bool) *appsv1.Deployment {
	if _, err := fw.PatchDeployment(deploymentName, namespace, patchString); err != nil {
		if !expectCreateFail {
			t.Fatalf("Error creating %q deployment in %v: %v", deploymentName, namespace, err)
		} else {
			return nil
		}
	}
	fw.WaitForDeploymentPods(deploymentName, namespace, time.Minute)
	deployment, err := fw.GetDeployment(deploymentName, namespace)
	if err != nil {
		t.Fatalf("Error getting %q deployment in %v: %v", deploymentName, namespace, err)
	}
	return deployment
}
func replaceDeployment(t *testing.T, fw *framework.Framework, namespace, manifestLocation string, expectCreateFail bool) *appsv1.Deployment {
	manifest, err := fw.LoadDeploymentManifest(manifestLocation)
	if err != nil {
		t.Fatalf("Error loading manifest: %v", err)
	}
	if manifest == nil {
		t.Fatalf("Error loading manifest: manifest is nil")
	}
	if _, err := fw.ReplaceDeployment(namespace, manifest); err != nil {
		if !expectCreateFail {
			t.Fatalf("Error creating %q deployment in %v: %v", manifest.Name, namespace, err)
		} else {
			return nil
		}
	}
	fw.WaitForDeploymentPods(manifest.Name, namespace, time.Minute)
	deployment, err := fw.GetDeployment(manifest.Name, namespace)
	if err != nil {
		t.Fatalf("Error getting %q deployment in %v: %v", manifest.Name, namespace, err)
	}
	return deployment
}
type ShaCheck int
const (
	NoCheck = iota
	VerifySha
	VerifyNoSha
)
func TestDeploymentRunnableCheck(t *testing.T, fw *framework.Framework, manifestLocation, namespace string, sCheck ShaCheck) {
	deployment := buildDeployment(t, fw, manifestLocation, namespace, false)
	defer fw.DeleteDeployment(deployment.Name, deployment.Namespace)
	switch sCheck {
	case NoCheck:
		break
	case VerifySha:
		err := allImagesContain(deployment.Spec.Template, "@sha256:")
		if !assert.NoError(t, err) {
			DumpEvents(t, fw, namespace)
			DumpPolicies(t, fw, namespace)
		}
	case VerifyNoSha:
		err := noImagesContain(deployment.Spec.Template, "@sha256:")
		if !assert.NoError(t, err) {
			DumpEvents(t, fw, namespace)
			DumpPolicies(t, fw, namespace)
		}
	}
	if !assert.Equal(t, *deployment.Spec.Replicas, deployment.Status.AvailableReplicas, "Deployment failed: available replicas did not match expected replicas") {
		DumpEvents(t, fw, namespace)
		DumpPolicies(t, fw, namespace)
	}
}
func allImagesContain(template v1.PodTemplateSpec, content string) error {
	for _, c := range template.Spec.InitContainers {
		if !strings.Contains(c.Image, content) {
			return fmt.Errorf("container %s has image without %s", c.Name, content)
		}
	}
	for _, c := range template.Spec.Containers {
		if !strings.Contains(c.Image, content) {
			return fmt.Errorf("container %s has image without %s", c.Name, content)
		}
	}
	return nil
}
func noImagesContain(template v1.PodTemplateSpec, content string) error {
	for _, c := range template.Spec.InitContainers {
		if strings.Contains(c.Image, content) {
			return fmt.Errorf("container %s has image with %s", c.Name, content)
		}
	}
	for _, c := range template.Spec.Containers {
		if strings.Contains(c.Image, content) {
			return fmt.Errorf("container %s has image with %s", c.Name, content)
		}
	}
	return nil
}