77
88 pb "github.com/nginx/agent/v3/api/grpc/mpi/v1"
99 . "github.com/onsi/gomega"
10+ v1 "k8s.io/api/core/v1"
1011 "k8s.io/apimachinery/pkg/types"
1112
1213 "github.com/nginx/nginx-gateway-fabric/v2/internal/controller/nginx/agent/broadcast"
@@ -44,7 +45,7 @@ func TestSetAndGetFiles(t *testing.T) {
4445 },
4546 }
4647
47- msg := deployment .SetFiles (files )
48+ msg := deployment .SetFiles (files , []v1. VolumeMount {} )
4849 fileOverviews , configVersion := deployment .GetFileOverviews ()
4950
5051 g .Expect (msg .Type ).To (Equal (broadcast .ConfigApplyRequest ))
@@ -61,7 +62,87 @@ func TestSetAndGetFiles(t *testing.T) {
6162 g .Expect (wrongHashFile ).To (BeNil ())
6263
6364 // Set the same files again
64- msg = deployment .SetFiles (files )
65+ msg = deployment .SetFiles (files , []v1.VolumeMount {})
66+ g .Expect (msg ).To (BeNil ())
67+
68+ newFileOverviews , _ := deployment .GetFileOverviews ()
69+ g .Expect (newFileOverviews ).To (Equal (fileOverviews ))
70+ }
71+
72+ func TestSetAndGetFiles_VolumeIgnoreFiles (t * testing.T ) {
73+ t .Parallel ()
74+ g := NewWithT (t )
75+
76+ deployment := newDeployment (& broadcastfakes.FakeBroadcaster {})
77+
78+ // Set up latestFileNames that will match with volume mount paths
79+ deployment .latestFileNames = []string {
80+ "/var/log/nginx/access.log" ,
81+ "/var/log/nginx/error.log" ,
82+ "/etc/ssl/certs/cert.pem" ,
83+ "/etc/nginx/conf.d/default.conf" , // This won't match any volume mount
84+ }
85+
86+ files := []File {
87+ {
88+ Meta : & pb.FileMeta {
89+ Name : "test.conf" ,
90+ Hash : "12345" ,
91+ },
92+ Contents : []byte ("test content" ),
93+ },
94+ }
95+
96+ // Create volume mounts that will match some of the latestFileNames
97+ volumeMounts := []v1.VolumeMount {
98+ {
99+ Name : "log-volume" ,
100+ MountPath : "/var/log/nginx" ,
101+ },
102+ {
103+ Name : "ssl-volume" ,
104+ MountPath : "/etc/ssl" ,
105+ },
106+ }
107+
108+ msg := deployment .SetFiles (files , volumeMounts )
109+ fileOverviews , configVersion := deployment .GetFileOverviews ()
110+
111+ g .Expect (msg .Type ).To (Equal (broadcast .ConfigApplyRequest ))
112+ g .Expect (msg .ConfigVersion ).To (Equal (configVersion ))
113+
114+ // Expected files: 1 managed file + 8 ignoreFiles + 3 volumeIgnoreFiles
115+ // (3 files from latestFileNames that match volume mount paths)
116+ g .Expect (msg .FileOverviews ).To (HaveLen (12 ))
117+ g .Expect (fileOverviews ).To (Equal (msg .FileOverviews ))
118+
119+ // Verify managed file
120+ file , _ := deployment .GetFile ("test.conf" , "12345" )
121+ g .Expect (file ).To (Equal ([]byte ("test content" )))
122+
123+ // Check that volume ignore files are present in the unmanaged files
124+ unmanagedFiles := make ([]string , 0 )
125+ for _ , overview := range msg .FileOverviews {
126+ if overview .Unmanaged {
127+ unmanagedFiles = append (unmanagedFiles , overview .FileMeta .Name )
128+ }
129+ }
130+
131+ // Should contain files that match volume mount paths
132+ g .Expect (unmanagedFiles ).To (ContainElement ("/var/log/nginx/access.log" ))
133+ g .Expect (unmanagedFiles ).To (ContainElement ("/var/log/nginx/error.log" ))
134+ g .Expect (unmanagedFiles ).To (ContainElement ("/etc/ssl/certs/cert.pem" ))
135+
136+ // Should NOT contain file that doesn't match volume mount paths
137+ g .Expect (unmanagedFiles ).ToNot (ContainElement ("/etc/nginx/conf.d/default.conf" ))
138+
139+ invalidFile , _ := deployment .GetFile ("invalid" , "12345" )
140+ g .Expect (invalidFile ).To (BeNil ())
141+ wrongHashFile , _ := deployment .GetFile ("test.conf" , "invalid" )
142+ g .Expect (wrongHashFile ).To (BeNil ())
143+
144+ // Set the same files again
145+ msg = deployment .SetFiles (files , volumeMounts )
65146 g .Expect (msg ).To (BeNil ())
66147
67148 newFileOverviews , _ := deployment .GetFileOverviews ()
0 commit comments