66 "fmt"
77 "math/rand"
88 "os"
9+ "regexp"
910 "strings"
1011 "time"
1112
@@ -22,6 +23,7 @@ import (
2223
2324 machineconfigclient "github.com/openshift/client-go/machineconfiguration/clientset/versioned"
2425 exutil "github.com/openshift/origin/test/extended/util"
26+ "github.com/openshift/origin/test/extended/util/compat_otp"
2527)
2628
2729// getNodesByLabel returns nodes matching the specified label selector
@@ -671,3 +673,84 @@ func ensureDropInDirectoryExists(ctx context.Context, oc *exutil.CLI, dirPath st
671673
672674 return nil
673675}
676+
677+ // ============================================================================
678+ // Helper functions for testcases migrated from openshift-tests-private
679+ // ============================================================================
680+
681+ // PodInitConDescription describes a pod with init container for testing
682+ type PodInitConDescription struct {
683+ Name string
684+ Namespace string
685+ Template string
686+ }
687+
688+ func (p * PodInitConDescription ) Create (oc * exutil.CLI ) {
689+ configFile := compat_otp .ProcessTemplate (oc , "--ignore-unknown-parameters=true" , "-f" , p .Template , "-p" , "NAME=" + p .Name , "NAMESPACE=" + p .Namespace )
690+ err := oc .AsAdmin ().Run ("create" ).Args ("-f" , configFile ).Execute ()
691+ o .Expect (err ).NotTo (o .HaveOccurred ())
692+ }
693+
694+ func (p * PodInitConDescription ) Delete (oc * exutil.CLI ) {
695+ err := oc .AsAdmin ().Run ("delete" ).Args ("-n" , p .Namespace , "pod" , p .Name ).Execute ()
696+ o .Expect (err ).NotTo (o .HaveOccurred ())
697+ }
698+
699+ func (p * PodInitConDescription ) ContainerExit (oc * exutil.CLI ) error {
700+ return wait .Poll (2 * time .Second , 2 * time .Minute , func () (bool , error ) {
701+ initConStatus , err := oc .AsAdmin ().Run ("get" ).Args ("pod" , "-o=jsonpath={.items[0].status.initContainerStatuses[0].state.terminated.reason}" , "-n" , p .Namespace ).Output ()
702+ framework .Logf ("The initContainer status is %v" , initConStatus )
703+ o .Expect (err ).NotTo (o .HaveOccurred ())
704+ if strings .Contains (initConStatus , "Completed" ) {
705+ framework .Logf ("The initContainer exit normally" )
706+ return true , nil
707+ }
708+ framework .Logf ("The initContainer not exit!" )
709+ return false , nil
710+ })
711+ }
712+
713+ func (p * PodInitConDescription ) DeleteInitContainer (oc * exutil.CLI ) (string , error ) {
714+ nodename , err := oc .AsAdmin ().Run ("get" ).Args ("pod" , "-o=jsonpath={.items[0].spec.nodeName}" , "-n" , p .Namespace ).Output ()
715+ o .Expect (err ).NotTo (o .HaveOccurred ())
716+ containerID , err := oc .AsAdmin ().Run ("get" ).Args ("pod" , "-o=jsonpath={.items[0].status.initContainerStatuses[0].containerID}" , "-n" , p .Namespace ).Output ()
717+ o .Expect (err ).NotTo (o .HaveOccurred ())
718+ framework .Logf ("The containerID is %v" , containerID )
719+ initContainerID := string (containerID )[8 :]
720+ framework .Logf ("The initContainerID is %s" , initContainerID )
721+ return compat_otp .DebugNodeWithChroot (oc , fmt .Sprintf ("%s" , nodename ), "crictl" , "rm" , initContainerID )
722+ }
723+
724+ func (p * PodInitConDescription ) InitContainerNotRestart (oc * exutil.CLI ) error {
725+ return wait .Poll (3 * time .Minute , 6 * time .Minute , func () (bool , error ) {
726+ re := regexp .MustCompile ("running" )
727+ podname , err := oc .AsAdmin ().Run ("get" ).Args ("pod" , "-o=jsonpath={.items[0].metadata.name}" , "-n" , p .Namespace ).Output ()
728+ o .Expect (err ).NotTo (o .HaveOccurred ())
729+ output , err := oc .AsAdmin ().Run ("exec" ).Args (string (podname ), "-n" , p .Namespace , "--" , "cat" , "/mnt/data/test" ).Output ()
730+ framework .Logf ("The /mnt/data/test: %s" , output )
731+ o .Expect (err ).NotTo (o .HaveOccurred ())
732+ found := re .FindAllString (output , - 1 )
733+ if lenStr := len (found ); lenStr > 1 {
734+ framework .Logf ("initContainer restart %d times." , (lenStr - 1 ))
735+ return false , nil
736+ } else if lenStr == 1 {
737+ framework .Logf ("initContainer not restart" )
738+ return true , nil
739+ }
740+ return false , nil
741+ })
742+ }
743+
744+ // PodStatus checks if pod is in Ready state
745+ func PodStatus (oc * exutil.CLI , namespace string , podName string ) error {
746+ framework .Logf ("check if pod is available" )
747+ return wait .Poll (5 * time .Second , 3 * time .Minute , func () (bool , error ) {
748+ status , err := oc .AsAdmin ().Run ("get" ).Args ("pod" , podName , "-o=jsonpath={.status.conditions[?(@.type=='Ready')].status}" , "-n" , namespace ).Output ()
749+ o .Expect (err ).NotTo (o .HaveOccurred ())
750+ if strings .Contains (status , "True" ) {
751+ framework .Logf ("Pod is running and container is Ready!" )
752+ return true , nil
753+ }
754+ return false , nil
755+ })
756+ }
0 commit comments