3232import org .apache .cloudstack .engine .subsystem .api .storage .DataStore ;
3333import org .apache .cloudstack .engine .subsystem .api .storage .DataStoreManager ;
3434import org .apache .cloudstack .engine .subsystem .api .storage .Scope ;
35+ import org .apache .cloudstack .storage .datastore .db .ImageStoreDao ;
36+ import org .apache .cloudstack .storage .datastore .db .ImageStoreVO ;
3537import org .apache .cloudstack .storage .datastore .db .TemplateDataStoreDao ;
3638import org .apache .cloudstack .storage .datastore .db .TemplateDataStoreVO ;
3739import org .apache .cloudstack .storage .image .store .TemplateObject ;
@@ -104,6 +106,12 @@ public class TemplateServiceImplTest {
104106 @ Mock
105107 DataCenterDao _dcDao ;
106108
109+ @ Mock
110+ ImageStoreDao imageStore ;
111+
112+ @ Mock
113+ ImageStoreVO imageMock ;
114+
107115 Map <String , TemplateProp > templatesInSourceStore = new HashMap <>();
108116
109117 @ Before
@@ -119,45 +127,59 @@ public void setUp() {
119127 Mockito .doReturn (templateInfoMock ).when (templateDataFactoryMock ).getTemplate (2L , sourceStoreMock );
120128 Mockito .doReturn (3L ).when (dataStoreMock ).getId ();
121129 Mockito .doReturn (zoneScopeMock ).when (dataStoreMock ).getScope ();
130+ Mockito .when (imageStore .findById (3L )).thenReturn (imageMock );
122131 }
123132
124133 @ Test
125134 public void shouldDownloadTemplateToStoreTestSkipsTemplateDirectedToAnotherStorage () {
126135 DataStore destinedStore = Mockito .mock (DataStore .class );
136+ Mockito .when (imageMock .isReadonly ()).thenReturn (false );
127137 Mockito .doReturn (dataStoreMock .getId () + 1L ).when (destinedStore ).getId ();
128138 Mockito .when (templateManagerMock .verifyHeuristicRulesForZone (tmpltMock , zoneScopeMock .getScopeId ())).thenReturn (destinedStore );
129139 Assert .assertFalse (templateService .shouldDownloadTemplateToStore (tmpltMock , dataStoreMock ));
130140 }
131141
132142 @ Test
133143 public void shouldDownloadTemplateToStoreTestDownloadsPublicTemplate () {
144+ Mockito .when (imageMock .isReadonly ()).thenReturn (false );
134145 Mockito .when (tmpltMock .isPublicTemplate ()).thenReturn (true );
135146 Assert .assertTrue (templateService .shouldDownloadTemplateToStore (tmpltMock , dataStoreMock ));
136147 }
137148
138149 @ Test
139150 public void shouldDownloadTemplateToStoreTestDownloadsFeaturedTemplate () {
151+ Mockito .when (imageMock .isReadonly ()).thenReturn (false );
140152 Mockito .when (tmpltMock .isFeatured ()).thenReturn (true );
141153 Assert .assertTrue (templateService .shouldDownloadTemplateToStore (tmpltMock , dataStoreMock ));
142154 }
143155
144156 @ Test
145157 public void shouldDownloadTemplateToStoreTestDownloadsSystemTemplate () {
158+ Mockito .when (imageMock .isReadonly ()).thenReturn (false );
146159 Mockito .when (tmpltMock .getTemplateType ()).thenReturn (Storage .TemplateType .SYSTEM );
147160 Assert .assertTrue (templateService .shouldDownloadTemplateToStore (tmpltMock , dataStoreMock ));
148161 }
149162
150163 @ Test
151164 public void shouldDownloadTemplateToStoreTestDownloadsPrivateNoRefTemplate () {
165+ Mockito .when (imageMock .isReadonly ()).thenReturn (false );
152166 Assert .assertTrue (templateService .shouldDownloadTemplateToStore (tmpltMock , dataStoreMock ));
153167 }
154168
155169 @ Test
156170 public void shouldDownloadTemplateToStoreTestSkipsPrivateExistingTemplate () {
171+ Mockito .when (imageMock .isReadonly ()).thenReturn (false );
157172 Mockito .when (templateDataStoreDao .findByTemplateZone (tmpltMock .getId (), zoneScopeMock .getScopeId (), DataStoreRole .Image )).thenReturn (Mockito .mock (TemplateDataStoreVO .class ));
158173 Assert .assertFalse (templateService .shouldDownloadTemplateToStore (tmpltMock , dataStoreMock ));
159174 }
160175
176+ @ Test
177+ public void shouldDownloadTemplateToStoreTestSkipsWhenStorageIsReadOnly () {
178+ Mockito .when (imageMock .isReadonly ()).thenReturn (true );
179+ Assert .assertFalse (templateService .shouldDownloadTemplateToStore (tmpltMock , dataStoreMock ));
180+
181+ }
182+
161183 @ Test
162184 public void tryDownloadingTemplateToImageStoreTestDownloadsTemplateWhenUrlIsNotNull () {
163185 Mockito .doReturn ("url" ).when (tmpltMock ).getUrl ();
0 commit comments