@@ -17,8 +17,13 @@ limitations under the License.
1717package server
1818
1919import (
20+ "bytes"
21+ "encoding/base64"
22+ "encoding/json"
23+ "net/http"
2024 "testing"
2125
26+ values "github.com/rancher/wrangler/pkg/data"
2227 "gotest.tools/assert"
2328)
2429
@@ -95,3 +100,58 @@ func TestBuildName(t *testing.T) {
95100 assert .Equal (t , testCase .Output , buildName (data , testCase .Format ))
96101 }
97102}
103+
104+ func TestSmbios (t * testing.T ) {
105+ dmiEncoded := map [string ]interface {}{}
106+ values .PutValue (dmiEncoded , "Myself" , "System Information" , "Manufacturer" )
107+ var buf bytes.Buffer
108+ b64Enc := base64 .NewEncoder (base64 .StdEncoding , & buf )
109+ json .NewEncoder (b64Enc ).Encode (dmiEncoded )
110+ _ = b64Enc .Close ()
111+
112+ testCase := []struct {
113+ header http.Header
114+ path []string // Path to the value
115+ value string // Actual value
116+ gotIt bool // Did we get the value
117+ }{
118+ {
119+ http.Header {"X-Cattle-Smbios" : {buf .String ()}}, // Old header
120+ []string {"System Information" , "Manufacturer" },
121+ "Myself" ,
122+ true ,
123+ },
124+ {
125+ http.Header {"X-Cattle-Smbios-1" : {buf .String ()}}, // New header
126+ []string {"System Information" , "Manufacturer" },
127+ "Myself" ,
128+ true ,
129+ },
130+ {
131+ http.Header {"X-Cattle-Smbios-2" : {buf .String ()}}, // New header but missing the first part
132+ []string {"System Information" , "Manufacturer" },
133+ "" ,
134+ false ,
135+ },
136+ {
137+ http.Header {}, // Empty header
138+ []string {"System Information" , "Manufacturer" },
139+ "" ,
140+ false ,
141+ },
142+ }
143+
144+ for _ , test := range testCase {
145+ data , err := getSMBios (& http.Request {Header : test .header })
146+ assert .Equal (t , err , nil )
147+ d , gotIt := values .GetValue (data , test .path ... )
148+ assert .Equal (t , gotIt , test .gotIt )
149+ // Cant compare string and nil and values.GetValue returns either a string or a nil
150+ if test .value == "" {
151+ assert .Equal (t , d , nil )
152+ } else {
153+ assert .Equal (t , d , test .value )
154+ }
155+
156+ }
157+ }
0 commit comments