@@ -3,6 +3,7 @@ package check
33import (
44 "context"
55 "errors"
6+ "strings"
67 "testing"
78)
89
@@ -47,3 +48,67 @@ func TestComposeCheck_NoServices(t *testing.T) {
4748 t .Errorf ("expected pass, got %v: %s" , result .Status , result .Message )
4849 }
4950}
51+
52+ func TestComposeImageCheck_AllPulled (t * testing.T ) {
53+ c := & ComposeImageCheck {runner : func () ([]byte , error ) {
54+ return []byte (`[{"ContainerName":"app_web_1","Repository":"nginx","ID":"sha256:abc"}]` ), nil
55+ }}
56+ result := c .Run (context .Background ())
57+ if result .Status != StatusPass {
58+ t .Errorf ("expected pass, got %v: %s" , result .Status , result .Message )
59+ }
60+ }
61+
62+ func TestComposeImageCheck_MissingImage (t * testing.T ) {
63+ c := & ComposeImageCheck {runner : func () ([]byte , error ) {
64+ return []byte (`[{"ContainerName":"app_web_1","Repository":"","ID":""}]` ), nil
65+ }}
66+ result := c .Run (context .Background ())
67+ if result .Status != StatusFail {
68+ t .Errorf ("expected fail, got %v" , result .Status )
69+ }
70+ if ! strings .Contains (result .Message , "app_web_1" ) {
71+ t .Errorf ("expected message to mention service name, got: %s" , result .Message )
72+ }
73+ }
74+
75+ func TestComposeImageCheck_NoneRepository (t * testing.T ) {
76+ c := & ComposeImageCheck {runner : func () ([]byte , error ) {
77+ return []byte (`[{"ContainerName":"app_db_1","Repository":"<none>","ID":""}]` ), nil
78+ }}
79+ result := c .Run (context .Background ())
80+ if result .Status != StatusFail {
81+ t .Errorf ("expected fail, got %v" , result .Status )
82+ }
83+ }
84+
85+ func TestComposeImageCheck_JSONLFormat (t * testing.T ) {
86+ c := & ComposeImageCheck {runner : func () ([]byte , error ) {
87+ return []byte (`{"ContainerName":"app_web_1","Repository":"nginx","ID":"sha256:abc"}` + "\n " +
88+ `{"ContainerName":"app_db_1","Repository":"postgres","ID":"sha256:def"}` + "\n " ), nil
89+ }}
90+ result := c .Run (context .Background ())
91+ if result .Status != StatusPass {
92+ t .Errorf ("expected pass, got %v: %s" , result .Status , result .Message )
93+ }
94+ }
95+
96+ func TestComposeImageCheck_CommandFails (t * testing.T ) {
97+ c := & ComposeImageCheck {runner : func () ([]byte , error ) {
98+ return nil , errors .New ("docker not found" )
99+ }}
100+ result := c .Run (context .Background ())
101+ if result .Status != StatusFail {
102+ t .Errorf ("expected fail, got %v" , result .Status )
103+ }
104+ }
105+
106+ func TestComposeImageCheck_NoOutput (t * testing.T ) {
107+ c := & ComposeImageCheck {runner : func () ([]byte , error ) {
108+ return []byte ("" ), nil
109+ }}
110+ result := c .Run (context .Background ())
111+ if result .Status != StatusPass {
112+ t .Errorf ("expected pass, got %v: %s" , result .Status , result .Message )
113+ }
114+ }
0 commit comments