@@ -2,16 +2,137 @@ package config
22
33import (
44 "context"
5+ "fmt"
56 "os"
67 "strings"
78 "testing"
9+ "time"
810
911 "github.com/google/uuid"
12+ pb "go.viam.com/api/app/v1"
1013 "go.viam.com/test"
1114
15+ "go.viam.com/rdk/config/testutils"
1216 "go.viam.com/rdk/logging"
1317)
1418
19+ func TestFromReader (t * testing.T ) {
20+ const (
21+ robotPartID = "forCachingTest"
22+ secret = testutils .FakeCredentialPayLoad
23+ )
24+ var (
25+ logger = logging .NewTestLogger (t )
26+ ctx = context .Background ()
27+ )
28+
29+ // clear cache
30+ setupClearCache := func (t * testing.T ) {
31+ t .Helper ()
32+ clearCache (robotPartID )
33+ _ , err := readFromCache (robotPartID )
34+ test .That (t , os .IsNotExist (err ), test .ShouldBeTrue )
35+ }
36+
37+ setupFakeServer := func (t * testing.T ) (* testutils.FakeCloudServer , func ()) {
38+ t .Helper ()
39+
40+ logger := logging .NewTestLogger (t )
41+
42+ fakeServer , err := testutils .NewFakeCloudServer (context .Background (), logger )
43+ test .That (t , err , test .ShouldBeNil )
44+ cleanup := func () {
45+ test .That (t , fakeServer .Shutdown (), test .ShouldBeNil )
46+ }
47+
48+ return fakeServer , cleanup
49+ }
50+
51+ t .Run ("online" , func (t * testing.T ) {
52+ setupClearCache (t )
53+
54+ fakeServer , cleanup := setupFakeServer (t )
55+ defer cleanup ()
56+
57+ cloudResponse := & Cloud {
58+ ManagedBy : "acme" ,
59+ SignalingAddress : "abc" ,
60+ ID : robotPartID ,
61+ Secret : secret ,
62+ FQDN : "fqdn" ,
63+ LocalFQDN : "localFqdn" ,
64+ LocationSecrets : []LocationSecret {},
65+ LocationID : "the-location" ,
66+ PrimaryOrgID : "the-primary-org" ,
67+ }
68+ certProto := & pb.CertificateResponse {
69+ TlsCertificate : "cert" ,
70+ TlsPrivateKey : "key" ,
71+ }
72+
73+ cloudConfProto , err := CloudConfigToProto (cloudResponse )
74+ test .That (t , err , test .ShouldBeNil )
75+ protoConfig := & pb.RobotConfig {Cloud : cloudConfProto }
76+ fakeServer .StoreDeviceConfig (robotPartID , protoConfig , certProto )
77+
78+ appAddress := fmt .Sprintf ("http://%s" , fakeServer .Addr ().String ())
79+ cfgText := fmt .Sprintf (`{"cloud":{"id":%q,"app_address":%q,"secret":%q}}` , robotPartID , appAddress , secret )
80+ gotCfg , err := FromReader (ctx , "" , strings .NewReader (cfgText ), logger )
81+ defer clearCache (robotPartID )
82+ test .That (t , err , test .ShouldBeNil )
83+
84+ expectedCloud := * cloudResponse
85+ expectedCloud .AppAddress = appAddress
86+ expectedCloud .TLSCertificate = certProto .TlsCertificate
87+ expectedCloud .TLSPrivateKey = certProto .TlsPrivateKey
88+ expectedCloud .RefreshInterval = time .Duration (10000000000 )
89+ test .That (t , gotCfg .Cloud , test .ShouldResemble , & expectedCloud )
90+
91+ cachedCfg , err := readFromCache (robotPartID )
92+ test .That (t , err , test .ShouldBeNil )
93+ expectedCloud .AppAddress = ""
94+ test .That (t , cachedCfg .Cloud , test .ShouldResemble , & expectedCloud )
95+ })
96+
97+ t .Run ("offline with cached config" , func (t * testing.T ) {
98+ setupClearCache (t )
99+
100+ cachedCloud := & Cloud {
101+ ManagedBy : "acme" ,
102+ SignalingAddress : "abc" ,
103+ ID : robotPartID ,
104+ Secret : secret ,
105+ FQDN : "fqdn" ,
106+ LocalFQDN : "localFqdn" ,
107+ TLSCertificate : "cert" ,
108+ TLSPrivateKey : "key" ,
109+ LocationID : "the-location" ,
110+ PrimaryOrgID : "the-primary-org" ,
111+ }
112+ cachedConf := & Config {Cloud : cachedCloud }
113+ err := storeToCache (robotPartID , cachedConf )
114+ test .That (t , err , test .ShouldBeNil )
115+ defer clearCache (robotPartID )
116+
117+ fakeServer , cleanup := setupFakeServer (t )
118+ defer cleanup ()
119+ fakeServer .FailOnConfigAndCertsWith (context .DeadlineExceeded )
120+ fakeServer .StoreDeviceConfig (robotPartID , nil , nil )
121+
122+ appAddress := fmt .Sprintf ("http://%s" , fakeServer .Addr ().String ())
123+ cfgText := fmt .Sprintf (`{"cloud":{"id":%q,"app_address":%q,"secret":%q}}` , robotPartID , appAddress , secret )
124+ gotCfg , err := FromReader (ctx , "" , strings .NewReader (cfgText ), logger )
125+ test .That (t , err , test .ShouldBeNil )
126+
127+ expectedCloud := * cachedCloud
128+ expectedCloud .AppAddress = appAddress
129+ expectedCloud .TLSCertificate = "cert"
130+ expectedCloud .TLSPrivateKey = "key"
131+ expectedCloud .RefreshInterval = time .Duration (10000000000 )
132+ test .That (t , gotCfg .Cloud , test .ShouldResemble , & expectedCloud )
133+ })
134+ }
135+
15136func TestStoreToCache (t * testing.T ) {
16137 logger := logging .NewTestLogger (t )
17138 ctx := context .Background ()
0 commit comments