summaryrefslogtreecommitdiffstats
path: root/src/test/java/org/openslx/libvirt/xml/LibvirtXmlDocumentTest.java
blob: 56ceeed4fe2258e0b913383ee5c03ca14877607d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package org.openslx.libvirt.xml;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.fail;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;

import org.apache.commons.io.FileUtils;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.config.Configurator;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;

class LibvirtXmlDocumentStub extends LibvirtXmlDocument
{
	public LibvirtXmlDocumentStub( File xml )
			throws LibvirtXmlDocumentException, LibvirtXmlSerializationException, LibvirtXmlValidationException
	{
		super( xml );
	}

	public LibvirtXmlDocumentStub( File xml, InputStream rngSchema )
			throws LibvirtXmlDocumentException, LibvirtXmlSerializationException, LibvirtXmlValidationException
	{
		super( xml, rngSchema );
	}
}

public class LibvirtXmlDocumentTest
{
	private static final String EMPTY = new String();

	@BeforeAll
	public static void setUp()
	{
		// disable logging with log4j
		Configurator.setRootLevel( Level.OFF );
	}

	private LibvirtXmlDocument newLibvirtXmlDocumentInstance( String xmlFileName )
	{
		LibvirtXmlDocument document = null;

		try {
			File xmlFile = LibvirtXmlTestResources.getLibvirtXmlFile( xmlFileName );
			document = new LibvirtXmlDocumentStub( xmlFile );
		} catch ( LibvirtXmlDocumentException | LibvirtXmlSerializationException | LibvirtXmlValidationException e ) {
			String errorMsg = new String( "Cannot prepare requested Libvirt XML file from the resources folder" );
			fail( errorMsg );
		}

		return document;
	}

	private LibvirtXmlDocument newLibvirtXmlDocumentValidationInstance( String xmlFileName, String rngSchemaFileName )
			throws LibvirtXmlValidationException
	{
		LibvirtXmlDocument document = null;

		try {
			File xmlFile = LibvirtXmlTestResources.getLibvirtXmlFile( xmlFileName );
			InputStream rngSchema = LibvirtXmlResources.getLibvirtRng( rngSchemaFileName );
			document = new LibvirtXmlDocumentStub( xmlFile, rngSchema );
		} catch ( LibvirtXmlDocumentException | LibvirtXmlSerializationException e ) {
			String errorMsg = new String( "Cannot prepare requested Libvirt XML file from the resources folder" );
			fail( errorMsg );
		}

		return document;
	}

	private static long countLines( Reader input ) throws IOException
	{
		final BufferedReader bfrContent = new BufferedReader( input );
		return bfrContent.lines().count();
	}

	public static long countLinesFromString( String input ) throws IOException
	{
		return LibvirtXmlDocumentTest.countLines( new StringReader( input ) );
	}

	public static long countLinesFromFile( File input ) throws IOException
	{
		return LibvirtXmlDocumentTest.countLines( new FileReader( input ) );
	}

	@Test
	@DisplayName( "Read libvirt XML file to String" )
	public void testReadXmlFileToString() throws LibvirtXmlSerializationException, IOException
	{
		LibvirtXmlDocument vm = this.newLibvirtXmlDocumentInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" );
		File originalXmlFile = LibvirtXmlTestResources.getLibvirtXmlFile( "qemu-kvm_default-ubuntu-20-04-vm.xml" );

		final String readXmlContent = vm.toXml();

		assertNotNull( readXmlContent );

		final long lengthReadXmlContent = LibvirtXmlDocumentTest.countLinesFromString( readXmlContent );
		final long lengthOriginalXmlContent = LibvirtXmlDocumentTest.countLinesFromFile( originalXmlFile );

		assertEquals( lengthOriginalXmlContent, lengthReadXmlContent );
	}

	@Test
	@DisplayName( "Read libvirt XML file to file" )
	public void testReadXmlFileToFile() throws LibvirtXmlSerializationException, IOException
	{
		LibvirtXmlDocument vm = this.newLibvirtXmlDocumentInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" );
		File originalXmlFile = LibvirtXmlTestResources.getLibvirtXmlFile( "qemu-kvm_default-ubuntu-20-04-vm.xml" );
		File readXmlFile = LibvirtXmlTestResources.createLibvirtXmlTempFile();

		vm.toXml( readXmlFile );

		final String readXmlContent = FileUtils.readFileToString( readXmlFile, StandardCharsets.UTF_8 );
		final String originalXmlContent = FileUtils.readFileToString( originalXmlFile, StandardCharsets.UTF_8 );

		assertNotNull( readXmlContent );

		final long lengthReadXmlContent = LibvirtXmlDocumentTest.countLinesFromString( readXmlContent );
		final long lengthOriginalXmlContent = LibvirtXmlDocumentTest.countLinesFromString( originalXmlContent );

		assertEquals( lengthOriginalXmlContent, lengthReadXmlContent );
	}

	@Test
	@DisplayName( "Validate correct libvirt XML file" )
	public void testValidateCorrectXmlFile()
	{
		Executable validateXmlDocument = () -> {
			this.newLibvirtXmlDocumentValidationInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml", "domain.rng" );
		};

		assertDoesNotThrow( validateXmlDocument );
	}

	@Test
	@DisplayName( "Validate incorrect libvirt XML file" )
	public void testValidateIncorrectXmlFile()
	{
		Executable validateXmlDocument = () -> {
			LibvirtXmlDocument doc = this.newLibvirtXmlDocumentValidationInstance( "qemu-kvm_default-ubuntu-20-04-vm-invalid.xml", "domain.rng" );
			doc.validateXml();
		};

		assertThrows( LibvirtXmlValidationException.class, validateXmlDocument );
	}

	@Test
	@DisplayName( "Get non-existent node from libvirt XML file" )
	public void testGetNonExistentElement()
	{
		LibvirtXmlDocument vm = this.newLibvirtXmlDocumentInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" );
		assertNull( vm.getRootXmlNode().getXmlElement( "info" ) );
	}

	@Test
	@DisplayName( "Set non-existent node in libvirt XML file" )
	public void testSetNonExistentElement()
	{
		LibvirtXmlDocument vm = this.newLibvirtXmlDocumentInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" );
		vm.getRootXmlNode().setXmlElement( "info" );
		assertNotNull( vm.getRootXmlNode().getXmlElement( "info" ) );
	}

	@Test
	@DisplayName( "Get non-existent element's value in libvirt XML file" )
	public void testGetNonExistentElementValue()
	{
		LibvirtXmlDocument vm = this.newLibvirtXmlDocumentInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" );
		assertNull( vm.getRootXmlNode().getXmlElementValue( "info" ) );
	}

	@Test
	@DisplayName( "Set non-existent element's value in libvirt XML file" )
	public void testSetNonExistentElementValue()
	{
		LibvirtXmlDocument vm = this.newLibvirtXmlDocumentInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" );
		vm.getRootXmlNode().setXmlElementValue( "info", "content" );
		assertEquals( "content", vm.getRootXmlNode().getXmlElementValue( "info" ) );
	}

	@Test
	@DisplayName( "Get empty element from libvirt XML file" )
	public void testGetEmptyElement()
	{
		LibvirtXmlDocument vm = this.newLibvirtXmlDocumentInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" );
		assertNotNull( vm.getRootXmlNode().getXmlElement( "features/acpi" ) );
	}

	@Test
	@DisplayName( "Set empty element in libvirt XML file" )
	public void testSetEmptyElement()
	{
		LibvirtXmlDocument vm = this.newLibvirtXmlDocumentInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" );
		vm.getRootXmlNode().setXmlElement( "features/acpi" );
		assertNotNull( vm.getRootXmlNode().getXmlElement( "features/acpi" ) );
	}

	@Test
	@DisplayName( "Get empty element's value from libvirt XML file" )
	public void testGetEmptyElementValue()
	{
		LibvirtXmlDocument vm = this.newLibvirtXmlDocumentInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" );
		assertEquals( EMPTY, vm.getRootXmlNode().getXmlElementValue( "features/acpi" ) );
	}

	@Test
	@DisplayName( "Set empty element's value in libvirt XML file" )
	public void testSetEmptyElementValue()
	{
		LibvirtXmlDocument vm = this.newLibvirtXmlDocumentInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" );
		vm.getRootXmlNode().setXmlElementValue( "features/acpi", "content" );
		assertEquals( "content", vm.getRootXmlNode().getXmlElementValue( "features/acpi" ) );
	}

	@Test
	@DisplayName( "Get non-existent element's attribute value from libvirt XML file" )
	public void testGetNonExistentElementAttributeValue()
	{
		LibvirtXmlDocument vm = this.newLibvirtXmlDocumentInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" );
		assertNull( vm.getRootXmlNode().getXmlElementAttributeValue( "info", "test" ) );
	}

	@Test
	@DisplayName( "Set non-existent element's attribute value from libvirt XML file" )
	public void testSetNonExistentElementAttributeValue()
	{
		LibvirtXmlDocument vm = this.newLibvirtXmlDocumentInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" );
		vm.getRootXmlNode().setXmlElementAttributeValue( "info", "test", "info" );
		assertEquals( "info", vm.getRootXmlNode().getXmlElementAttributeValue( "info", "test" ) );
	}

	@Test
	@DisplayName( "Get element's non-existent attribute value from libvirt XML file" )
	public void testGetElementNonExistentAttributeValue()
	{
		LibvirtXmlDocument vm = this.newLibvirtXmlDocumentInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" );
		assertNull( vm.getRootXmlNode().getXmlElementAttributeValue( "features/acpi", "test" ) );
	}

	@Test
	@DisplayName( "Set element's non-existent attribute value from libvirt XML file" )
	public void testSetElementNonExistentAttributeValue()
	{
		LibvirtXmlDocument vm = this.newLibvirtXmlDocumentInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" );
		vm.getRootXmlNode().setXmlElementAttributeValue( "features/acpi", "test", "info" );
		assertEquals( "info", vm.getRootXmlNode().getXmlElementAttributeValue( "features/acpi", "test" ) );
	}

	@Test
	@DisplayName( "Get element's attribute value from libvirt XML file" )
	public void testGetElementAttributeValue()
	{
		LibvirtXmlDocument vm = this.newLibvirtXmlDocumentInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" );
		assertEquals( "partial", vm.getRootXmlNode().getXmlElementAttributeValue( "cpu", "check" ) );
	}

	@Test
	@DisplayName( "Set element's attribute value from libvirt XML file" )
	public void testSetElementAttributeValue()
	{
		LibvirtXmlDocument vm = this.newLibvirtXmlDocumentInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" );
		vm.getRootXmlNode().setXmlElementAttributeValue( "cpu", "check", "full" );
		assertEquals( "full", vm.getRootXmlNode().getXmlElementAttributeValue( "cpu", "check" ) );
	}
}