@@ -113,6 +113,71 @@ public void testRemoveBackingIndex() {
113113 }
114114 }
115115
116+ public void testAddBackingIndex () {
117+ int numBackingIndices = randomIntBetween (2 , 32 );
118+ String dataStreamName = randomAlphaOfLength (10 ).toLowerCase (Locale .ROOT );
119+
120+ List <Index > indices = new ArrayList <>(numBackingIndices );
121+ for (int k = 1 ; k <= numBackingIndices ; k ++) {
122+ indices .add (new Index (DataStream .getDefaultBackingIndexName (dataStreamName , k ), UUIDs .randomBase64UUID (random ())));
123+ }
124+ DataStream original = new DataStream (dataStreamName , createTimestampField ("@timestamp" ), indices );
125+
126+ Index indexToAdd = new Index ("index-to-add" , UUIDs .randomBase64UUID (random ()));
127+ DataStream updated = original .addBackingIndex (indexToAdd );
128+ assertThat (updated .getName (), equalTo (original .getName ()));
129+ // adding a backing index does not change the generation (only rollover does)
130+ assertThat (updated .getGeneration (), equalTo (original .getGeneration ()));
131+ assertThat (updated .getTimeStampField (), equalTo (original .getTimeStampField ()));
132+ assertThat (updated .getIndices ().size (), equalTo (numBackingIndices + 1 ));
133+ // the new index is added to the front so the existing write index stays last
134+ assertThat (updated .getIndices ().get (0 ), equalTo (indexToAdd ));
135+ assertThat (updated .getIndices ().get (updated .getIndices ().size () - 1 ), equalTo (original .getIndices ().get (numBackingIndices - 1 )));
136+ for (int k = 0 ; k < numBackingIndices ; k ++) {
137+ assertThat (updated .getIndices ().get (k + 1 ), equalTo (original .getIndices ().get (k )));
138+ }
139+ }
140+
141+ public void testRemoveBackingIndexThrowsExceptionIfIndexNotPartOfDataStream () {
142+ int numBackingIndices = randomIntBetween (2 , 32 );
143+ String dataStreamName = randomAlphaOfLength (10 ).toLowerCase (Locale .ROOT );
144+
145+ List <Index > indices = new ArrayList <>(numBackingIndices );
146+ for (int k = 1 ; k <= numBackingIndices ; k ++) {
147+ indices .add (new Index (DataStream .getDefaultBackingIndexName (dataStreamName , k ), UUIDs .randomBase64UUID (random ())));
148+ }
149+ DataStream original = new DataStream (dataStreamName , createTimestampField ("@timestamp" ), indices );
150+
151+ Index standaloneIndex = new Index ("index-foo" , UUIDs .randomBase64UUID (random ()));
152+ IllegalArgumentException e = expectThrows (IllegalArgumentException .class , () -> original .removeBackingIndex (standaloneIndex ));
153+ assertThat (e .getMessage (), equalTo ("index [index-foo] is not part of data stream [" + dataStreamName + "]" ));
154+ }
155+
156+ public void testRemoveBackingIndexThrowsExceptionIfRemovingWriteIndex () {
157+ int numBackingIndices = randomIntBetween (2 , 32 );
158+ int writeIndexPosition = numBackingIndices - 1 ;
159+ String dataStreamName = randomAlphaOfLength (10 ).toLowerCase (Locale .ROOT );
160+
161+ List <Index > indices = new ArrayList <>(numBackingIndices );
162+ for (int k = 1 ; k <= numBackingIndices ; k ++) {
163+ indices .add (new Index (DataStream .getDefaultBackingIndexName (dataStreamName , k ), UUIDs .randomBase64UUID (random ())));
164+ }
165+ DataStream original = new DataStream (dataStreamName , createTimestampField ("@timestamp" ), indices );
166+
167+ Index writeIndex = indices .get (writeIndexPosition );
168+ IllegalArgumentException e = expectThrows (IllegalArgumentException .class , () -> original .removeBackingIndex (writeIndex ));
169+ assertThat (
170+ e .getMessage (),
171+ equalTo (
172+ "cannot remove backing index ["
173+ + writeIndex .getName ()
174+ + "] of data stream ["
175+ + dataStreamName
176+ + "] because it is the write index"
177+ )
178+ );
179+ }
180+
116181 public void testDefaultBackingIndexName () {
117182 // this test does little more than flag that changing the default naming convention for backing indices
118183 // will also require changing a lot of hard-coded values in REST tests and docs
0 commit comments