@@ -36,3 +36,177 @@ async fn empty_new_config_uses_tedge_toml_defaults() {
3636 "Device ID should come from tedge.toml defaults"
3737 ) ;
3838}
39+
40+ mod default_location_mode {
41+ use super :: * ;
42+
43+ mod prefers_tedge_toml {
44+ use tedge_config:: models:: CloudType ;
45+
46+ use super :: * ;
47+
48+ #[ tokio:: test]
49+ async fn mapper_config_is_not_created_if_tedge_toml_does_not_exist ( ) {
50+ let ttd = TempTedgeDir :: new ( ) ;
51+
52+ let tedge_config = TEdgeConfig :: load ( ttd. path ( ) ) . await . unwrap ( ) ;
53+ tedge_config
54+ . update_toml ( & |dto, _rdr| {
55+ dto. c8y . try_get_mut ( None , "c8y" ) . unwrap ( ) . url =
56+ Some ( "example.com" . parse ( ) . unwrap ( ) ) ;
57+ Ok ( ( ) )
58+ } )
59+ . await
60+ . unwrap ( ) ;
61+
62+ assert ! (
63+ !ttd. path( ) . join( "mappers" ) . exists( ) ,
64+ "mappers dir should not have been created"
65+ ) ;
66+ }
67+
68+ #[ tokio:: test]
69+ async fn mapper_config_is_created_for_new_profile_of_existing_separate_config_cloud ( ) {
70+ let ttd = TempTedgeDir :: new ( ) ;
71+
72+ let tedge_config = TEdgeConfig :: load ( ttd. path ( ) )
73+ . await
74+ . unwrap ( ) ;
75+ tedge_config
76+ . update_toml ( & |dto, _rdr| {
77+ dto. c8y . try_get_mut ( None , "c8y" ) . unwrap ( ) . url =
78+ Some ( "example.com" . parse ( ) . unwrap ( ) ) ;
79+ Ok ( ( ) )
80+ } )
81+ . await
82+ . unwrap ( ) ;
83+
84+ let tedge_config = TEdgeConfig :: load ( ttd. path ( ) )
85+ . await
86+ . unwrap ( ) ;
87+ tedge_config. migrate_mapper_config ( CloudType :: C8y ) . await . unwrap ( ) ;
88+
89+ let tedge_config = TEdgeConfig :: load ( ttd. path ( ) )
90+ . await
91+ . unwrap ( ) ;
92+ tedge_config
93+ . update_toml ( & |dto, _rdr| {
94+ dto. c8y . try_get_mut ( Some ( "new-profile" ) , "c8y" ) . unwrap ( ) . url =
95+ Some ( "new.example.com" . parse ( ) . unwrap ( ) ) ;
96+ Ok ( ( ) )
97+ } )
98+ . await
99+ . unwrap ( ) ;
100+
101+ assert ! (
102+ ttd. path( ) . join( "mappers" ) . exists( ) ,
103+ "mappers dir should exist"
104+ ) ;
105+ let c8y_toml = tokio:: fs:: read_to_string ( ttd. path ( ) . join ( "mappers/c8y.toml" ) )
106+ . await
107+ . unwrap ( ) ;
108+ assert_eq ! ( c8y_toml. trim( ) , "url = \" example.com\" " ) ;
109+ }
110+ }
111+
112+ mod prefers_separate_config {
113+ use super :: * ;
114+
115+ #[ tokio:: test]
116+ async fn mapper_config_is_created_if_tedge_toml_does_not_exist ( ) {
117+ let ttd = TempTedgeDir :: new ( ) ;
118+
119+ let tedge_config = TEdgeConfig :: load_prefer_separate_mapper_config ( ttd. path ( ) )
120+ . await
121+ . unwrap ( ) ;
122+ tedge_config
123+ . update_toml ( & |dto, _rdr| {
124+ dto. c8y . try_get_mut ( None , "c8y" ) . unwrap ( ) . url =
125+ Some ( "example.com" . parse ( ) . unwrap ( ) ) ;
126+ Ok ( ( ) )
127+ } )
128+ . await
129+ . unwrap ( ) ;
130+
131+ assert ! (
132+ ttd. path( ) . join( "mappers" ) . exists( ) ,
133+ "mappers dir should have been created"
134+ ) ;
135+
136+ let tedge_toml = tokio:: fs:: read_to_string ( ttd. path ( ) . join ( "tedge.toml" ) )
137+ . await
138+ . unwrap ( ) ;
139+ let c8y_toml = tokio:: fs:: read_to_string ( ttd. path ( ) . join ( "mappers/c8y.toml" ) )
140+ . await
141+ . unwrap ( ) ;
142+ assert_eq ! ( tedge_toml, "" ) ;
143+ assert_eq ! ( c8y_toml. trim( ) , "url = \" example.com\" " ) ;
144+ }
145+
146+ #[ tokio:: test]
147+ async fn mapper_config_is_created_if_tedge_toml_has_no_cloud_configs ( ) {
148+ let ttd = TempTedgeDir :: new ( ) ;
149+ ttd. file ( "tedge.toml" ) . with_toml_content ( toml:: toml!(
150+ device. type = "my-fancy-device"
151+ ) ) ;
152+
153+ let tedge_config = TEdgeConfig :: load_prefer_separate_mapper_config ( ttd. path ( ) )
154+ . await
155+ . unwrap ( ) ;
156+ tedge_config
157+ . update_toml ( & |dto, _rdr| {
158+ dto. c8y . try_get_mut ( None , "c8y" ) . unwrap ( ) . url =
159+ Some ( "example.com" . parse ( ) . unwrap ( ) ) ;
160+ Ok ( ( ) )
161+ } )
162+ . await
163+ . unwrap ( ) ;
164+
165+ assert ! (
166+ ttd. path( ) . join( "mappers" ) . exists( ) ,
167+ "mappers dir should have been created"
168+ ) ;
169+
170+ let tedge_toml = tokio:: fs:: read_to_string ( ttd. path ( ) . join ( "tedge.toml" ) )
171+ . await
172+ . unwrap ( ) ;
173+ let c8y_toml = tokio:: fs:: read_to_string ( ttd. path ( ) . join ( "mappers/c8y.toml" ) )
174+ . await
175+ . unwrap ( ) ;
176+ assert ! ( !tedge_toml. contains( "c8y" ) ) ;
177+ assert_eq ! ( c8y_toml. trim( ) , "url = \" example.com\" " ) ;
178+ }
179+
180+ #[ tokio:: test]
181+ async fn mapper_config_is_not_created_for_new_profile_of_existing_tedge_toml_cloud ( ) {
182+ let ttd = TempTedgeDir :: new ( ) ;
183+ ttd. file ( "tedge.toml" )
184+ . with_toml_content ( toml:: toml!( c8y. url = "example.com" ) ) ;
185+
186+ let tedge_config = TEdgeConfig :: load_prefer_separate_mapper_config ( ttd. path ( ) )
187+ . await
188+ . unwrap ( ) ;
189+ tedge_config
190+ . update_toml ( & |dto, _rdr| {
191+ dto. c8y . try_get_mut ( Some ( "new-profile" ) , "c8y" ) . unwrap ( ) . url =
192+ Some ( "new.example.com" . parse ( ) . unwrap ( ) ) ;
193+ Ok ( ( ) )
194+ } )
195+ . await
196+ . unwrap ( ) ;
197+
198+ assert ! (
199+ !ttd. path( ) . join( "mappers" ) . exists( ) ,
200+ "mappers dir should not have been created"
201+ ) ;
202+ }
203+ }
204+ }
205+
206+ // TEST PLAN
207+ // no tedge.toml, old mode -> no migration
208+ // tedge.toml with c8y, old mode -> no migration
209+ // tedge.toml with az, old mode -> no migration
210+ // no tedge.toml, new mode -> migrate
211+ // tedge.toml without c8y, new mode -> migrate
212+ // tedge.toml cannot be read -> do nothing
0 commit comments