This file is indexed.

/usr/share/gocode/src/github.com/lxc/lxd/lxd/db_test.go is in golang-github-lxc-lxd-dev 2.0.2-0ubuntu1~16.04.1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
package main

import (
	"database/sql"
	"fmt"
	"testing"
	"time"

	"github.com/lxc/lxd/shared"
	"github.com/lxc/lxd/shared/logging"
)

const DB_FIXTURES string = `
    INSERT INTO containers (name, architecture, type) VALUES ('thename', 1, 1);
    INSERT INTO profiles (name) VALUES ('theprofile');
    INSERT INTO containers_profiles (container_id, profile_id) VALUES (1, 3);
    INSERT INTO containers_config (container_id, key, value) VALUES (1, 'thekey', 'thevalue');
    INSERT INTO containers_devices (container_id, name, type) VALUES (1, 'somename', 1);
    INSERT INTO containers_devices_config (key, value, container_device_id) VALUES ('configkey', 'configvalue', 1);
    INSERT INTO images (fingerprint, filename, size, architecture, creation_date, expiry_date, upload_date) VALUES ('fingerprint', 'filename', 1024, 0,  1431547174,  1431547175,  1431547176);
    INSERT INTO images_aliases (name, image_id, description) VALUES ('somealias', 1, 'some description');
    INSERT INTO images_properties (image_id, type, key, value) VALUES (1, 0, 'thekey', 'some value');
    INSERT INTO profiles_config (profile_id, key, value) VALUES (3, 'thekey', 'thevalue');
    INSERT INTO profiles_devices (profile_id, name, type) VALUES (3, 'devicename', 1);
    INSERT INTO profiles_devices_config (profile_device_id, key, value) VALUES (4, 'devicekey', 'devicevalue');
    `

//  This Helper will initialize a test in-memory DB.
func createTestDb(t *testing.T) (db *sql.DB) {
	// Setup logging if main() hasn't been called/when testing
	if shared.Log == nil {
		var err error
		shared.Log, err = logging.GetLogger("", "", true, true, nil)
		if err != nil {
			t.Fatal(err)
		}
	}

	var err error
	d := &Daemon{MockMode: true}
	err = initializeDbObject(d, ":memory:")
	db = d.db

	if err != nil {
		t.Fatal(err)
	}

	_, err = db.Exec(DB_FIXTURES)
	if err != nil {
		t.Fatal(err)
	}
	return // db is a named output param
}

func Test_deleting_a_container_cascades_on_related_tables(t *testing.T) {
	var db *sql.DB
	var err error
	var count int
	var statements string

	// Insert a container and a related profile.
	db = createTestDb(t)
	defer db.Close()

	// Drop the container we just created.
	statements = `DELETE FROM containers WHERE name = 'thename';`

	_, err = db.Exec(statements)
	if err != nil {
		t.Errorf("Error deleting container! %s", err)
	}

	// Make sure there are 0 container_profiles entries left.
	statements = `SELECT count(*) FROM containers_profiles;`
	err = db.QueryRow(statements).Scan(&count)

	if count != 0 {
		t.Errorf("Deleting a container didn't delete the profile association! There are %d left", count)
	}

	// Make sure there are 0 containers_config entries left.
	statements = `SELECT count(*) FROM containers_config;`
	err = db.QueryRow(statements).Scan(&count)

	if count != 0 {
		t.Errorf("Deleting a container didn't delete the associated container_config! There are %d left", count)
	}

	// Make sure there are 0 containers_devices entries left.
	statements = `SELECT count(*) FROM containers_devices;`
	err = db.QueryRow(statements).Scan(&count)

	if count != 0 {
		t.Errorf("Deleting a container didn't delete the associated container_devices! There are %d left", count)
	}

	// Make sure there are 0 containers_devices_config entries left.
	statements = `SELECT count(*) FROM containers_devices_config;`
	err = db.QueryRow(statements).Scan(&count)

	if count != 0 {
		t.Errorf("Deleting a container didn't delete the associated container_devices_config! There are %d left", count)
	}

}

func Test_deleting_a_profile_cascades_on_related_tables(t *testing.T) {
	var db *sql.DB
	var err error
	var count int
	var statements string

	// Insert a container and a related profile.
	db = createTestDb(t)
	defer db.Close()

	// Drop the profile we just created.
	statements = `DELETE FROM profiles WHERE name = 'theprofile';`

	_, err = db.Exec(statements)
	if err != nil {
		t.Errorf("Error deleting profile! %s", err)
	}

	// Make sure there are 0 container_profiles entries left.
	statements = `SELECT count(*) FROM containers_profiles WHERE profile_id = 3;`
	err = db.QueryRow(statements).Scan(&count)

	if count != 0 {
		t.Errorf("Deleting a profile didn't delete the container association! There are %d left", count)
	}

	// Make sure there are 0 profiles_devices entries left.
	statements = `SELECT count(*) FROM profiles_devices WHERE profile_id == 3;`
	err = db.QueryRow(statements).Scan(&count)

	if count != 0 {
		t.Errorf("Deleting a profile didn't delete the related profiles_devices! There are %d left", count)
	}

	// Make sure there are 0 profiles_config entries left.
	statements = `SELECT count(*) FROM profiles_config WHERE profile_id == 3;`
	err = db.QueryRow(statements).Scan(&count)

	if count != 0 {
		t.Errorf("Deleting a profile didn't delete the related profiles_config! There are %d left", count)
	}

	// Make sure there are 0 profiles_devices_config entries left.
	statements = `SELECT count(*) FROM profiles_devices_config WHERE profile_device_id == 4;`
	err = db.QueryRow(statements).Scan(&count)

	if count != 0 {
		t.Errorf("Deleting a profile didn't delete the related profiles_devices_config! There are %d left", count)
	}

}

func Test_deleting_an_image_cascades_on_related_tables(t *testing.T) {
	var db *sql.DB
	var err error
	var count int
	var statements string

	db = createTestDb(t)
	defer db.Close()

	// Drop the image we just created.
	statements = `DELETE FROM images;`

	_, err = db.Exec(statements)
	if err != nil {
		t.Errorf("Error deleting image! %s", err)
	}

	// Make sure there are 0 images_aliases entries left.
	statements = `SELECT count(*) FROM images_aliases;`
	err = db.QueryRow(statements).Scan(&count)

	if count != 0 {
		t.Errorf("Deleting an image didn't delete the image alias association! There are %d left", count)
	}

	// Make sure there are 0 images_properties entries left.
	statements = `SELECT count(*) FROM images_properties;`
	err = db.QueryRow(statements).Scan(&count)

	if count != 0 {
		t.Errorf("Deleting an image didn't delete the related images_properties! There are %d left", count)
	}
}

func Test_initializing_db_is_indempotent(t *testing.T) {
	var db *sql.DB
	var err error

	// This calls "createDb" once already.
	d := &Daemon{MockMode: true}
	err = initializeDbObject(d, ":memory:")
	db = d.db

	defer db.Close()

	// Let's call it a second time.
	err = createDb(db)
	if err != nil {
		t.Errorf("The database schema is not indempotent, err='%s'", err)
	}
}

func Test_get_schema_returns_0_on_uninitialized_db(t *testing.T) {
	var db *sql.DB
	var err error

	db, err = sql.Open("sqlite3", ":memory:")
	if err != nil {
		t.Error(err)
	}
	result := dbGetSchema(db)

	if result != 0 {
		t.Error("getSchema should return 0 on uninitialized db!")
	}
}

func Test_running_dbUpdateFromV6_adds_on_delete_cascade(t *testing.T) {
	// Upgrading the database schema with updateFromV6 adds ON DELETE CASCADE
	// to sqlite tables that require it, and conserve the data.

	var err error
	var count int

	d := &Daemon{MockMode: true}
	err = initializeDbObject(d, ":memory:")
	defer d.db.Close()

	statements := `
CREATE TABLE IF NOT EXISTS containers (
    id INTEGER primary key AUTOINCREMENT NOT NULL,
    name VARCHAR(255) NOT NULL,
    architecture INTEGER NOT NULL,
    type INTEGER NOT NULL,
    power_state INTEGER NOT NULL DEFAULT 0,
    ephemeral INTEGER NOT NULL DEFAULT 0,
    UNIQUE (name)
);
CREATE TABLE IF NOT EXISTS containers_config (
    id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    container_id INTEGER NOT NULL,
    key VARCHAR(255) NOT NULL,
    value TEXT,
    FOREIGN KEY (container_id) REFERENCES containers (id),
    UNIQUE (container_id, key)
);

INSERT INTO containers (name, architecture, type) VALUES ('thename', 1, 1);
INSERT INTO containers_config (container_id, key, value) VALUES (1, 'thekey', 'thevalue');`

	_, err = d.db.Exec(statements)
	if err != nil {
		t.Error(err)
	}

	// Run the upgrade from V6 code
	err = dbUpdateFromV6(d.db)

	// Make sure the inserted data is still there.
	statements = `SELECT count(*) FROM containers_config;`
	err = d.db.QueryRow(statements).Scan(&count)

	if count != 1 {
		t.Fatalf("There should be exactly one entry in containers_config! There are %d.", count)
	}

	// Drop the container.
	statements = `DELETE FROM containers WHERE name = 'thename';`

	_, err = d.db.Exec(statements)
	if err != nil {
		t.Errorf("Error deleting container! %s", err)
	}

	// Make sure there are 0 container_profiles entries left.
	statements = `SELECT count(*) FROM containers_profiles;`
	err = d.db.QueryRow(statements).Scan(&count)

	if count != 0 {
		t.Errorf("Deleting a container didn't delete the profile association! There are %d left", count)
	}
}

func Test_run_database_upgrades_with_some_foreign_keys_inconsistencies(t *testing.T) {
	var db *sql.DB
	var err error
	var count int
	var statements string

	db, err = sql.Open("sqlite3", ":memory:")
	defer db.Close()

	if err != nil {
		t.Fatal(err)
	}

	// This schema is a part of schema rev 1.
	statements = `
CREATE TABLE containers (
    id INTEGER primary key AUTOINCREMENT NOT NULL,
    name VARCHAR(255) NOT NULL,
    architecture INTEGER NOT NULL,
    type INTEGER NOT NULL,
    UNIQUE (name)
);
CREATE TABLE containers_config (
    id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    container_id INTEGER NOT NULL,
    key VARCHAR(255) NOT NULL,
    value TEXT,
    FOREIGN KEY (container_id) REFERENCES containers (id),
    UNIQUE (container_id, key)
);
CREATE TABLE schema (
    id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    version INTEGER NOT NULL,
    updated_at DATETIME NOT NULL,
    UNIQUE (version)
);
CREATE TABLE images (
    id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    fingerprint VARCHAR(255) NOT NULL,
    filename VARCHAR(255) NOT NULL,
    size INTEGER NOT NULL,
    public INTEGER NOT NULL DEFAULT 0,
    architecture INTEGER NOT NULL,
    creation_date DATETIME,
    expiry_date DATETIME,
    upload_date DATETIME NOT NULL,
    UNIQUE (fingerprint)
);
CREATE TABLE images_properties (
    id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    image_id INTEGER NOT NULL,
    type INTEGER NOT NULL,
    key VARCHAR(255) NOT NULL,
    value TEXT,
    FOREIGN KEY (image_id) REFERENCES images (id)
);
CREATE TABLE certificates (
    id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    fingerprint VARCHAR(255) NOT NULL,
    type INTEGER NOT NULL,
    name VARCHAR(255) NOT NULL,
    certificate TEXT NOT NULL,
    UNIQUE (fingerprint)
);
INSERT INTO schema (version, updated_at) values (1, "now");
INSERT INTO containers (name, architecture, type) VALUES ('thename', 1, 1);
INSERT INTO containers_config (container_id, key, value) VALUES (1, 'thekey', 'thevalue');`

	_, err = db.Exec(statements)
	if err != nil {
		t.Fatal("Error creating schema!")
	}

	// Now that we have a consistent schema, let's remove the container entry
	// *without* the ON DELETE CASCADE in place.
	statements = `DELETE FROM containers;`
	_, err = db.Exec(statements)
	if err != nil {
		t.Fatal("Error truncating the container table!")
	}

	// The "foreign key" on containers_config now points to nothing.
	// Let's run the schema upgrades.
	d := &Daemon{MockMode: true}
	d.db = db
	daemonConfigInit(db)

	err = dbUpdate(d, 1)
	if err != nil {
		t.Error("Error upgrading database schema!")
		t.Fatal(err)
	}

	result := dbGetSchema(db)
	if result != DB_CURRENT_VERSION {
		t.Fatal(fmt.Sprintf("The schema is not at the latest version after update! Found: %d, should be: %d", result, DB_CURRENT_VERSION))
	}

	// Make sure there are 0 containers_config entries left.
	statements = `SELECT count(*) FROM containers_config;`
	err = db.QueryRow(statements).Scan(&count)

	if count != 0 {
		t.Fatal("updateDb did not delete orphaned child entries after adding ON DELETE CASCADE!")
	}

}

func Test_dbImageGet_finds_image_for_fingerprint(t *testing.T) {
	var db *sql.DB
	var err error
	var result *shared.ImageInfo

	db = createTestDb(t)
	defer db.Close()

	_, result, err = dbImageGet(db, "fingerprint", false, false)

	if err != nil {
		t.Fatal(err)
	}

	if result == nil {
		t.Fatal("No image returned!")
	}

	if result.Filename != "filename" {
		t.Fatal("Filename should be set.")
	}

	if result.CreationDate.UTC() != time.Unix(1431547174, 0).UTC() {
		t.Fatal(fmt.Sprintf("%s != %s", result.CreationDate, time.Unix(1431547174, 0)))
	}

	if result.ExpiryDate.UTC() != time.Unix(1431547175, 0).UTC() { // It was short lived
		t.Fatal(fmt.Sprintf("%s != %s", result.ExpiryDate, time.Unix(1431547175, 0)))
	}

	if result.UploadDate.UTC() != time.Unix(1431547176, 0).UTC() {
		t.Fatal(fmt.Sprintf("%s != %s", result.UploadDate, time.Unix(1431547176, 0)))
	}
}

func Test_dbImageGet_for_missing_fingerprint(t *testing.T) {
	var db *sql.DB
	var err error

	db = createTestDb(t)
	defer db.Close()

	_, _, err = dbImageGet(db, "unknown", false, false)

	if err != sql.ErrNoRows {
		t.Fatal("Wrong err type returned")
	}
}

func Test_dbImageAliasGet_alias_exists(t *testing.T) {
	var db *sql.DB
	var err error
	var result string

	db = createTestDb(t)
	defer db.Close()

	_, alias, err := dbImageAliasGet(db, "somealias", true)
	result = alias.Target

	if err != nil {
		t.Fatal(err)
	}

	if result != "fingerprint" {
		t.Fatal("Fingerprint is not the expected fingerprint!")
	}

}

func Test_dbImageAliasGet_alias_does_not_exists(t *testing.T) {
	var db *sql.DB
	var err error

	db = createTestDb(t)
	defer db.Close()

	_, _, err = dbImageAliasGet(db, "whatever", true)

	if err != NoSuchObjectError {
		t.Fatal("Error should be NoSuchObjectError")
	}
}

func Test_dbImageAliasAdd(t *testing.T) {
	var db *sql.DB
	var err error
	var result string

	db = createTestDb(t)
	defer db.Close()

	err = dbImageAliasAdd(db, "Chaosphere", 1, "Someone will like the name")
	if err != nil {
		t.Fatal("Error inserting Image alias.")
	}

	_, alias, err := dbImageAliasGet(db, "Chaosphere", true)
	if err != nil {
		t.Fatal(err)
	}
	result = alias.Target

	if result != "fingerprint" {
		t.Fatal("Couldn't retrieve newly created alias.")
	}
}

func Test_dbContainerConfig(t *testing.T) {
	var db *sql.DB
	var err error
	var result map[string]string
	var expected map[string]string

	db = createTestDb(t)
	defer db.Close()

	_, err = db.Exec("INSERT INTO containers_config (container_id, key, value) VALUES (1, 'something', 'something else');")

	result, err = dbContainerConfig(db, 1)
	if err != nil {
		t.Fatal(err)
	}

	expected = map[string]string{"thekey": "thevalue", "something": "something else"}

	for key, value := range expected {
		if result[key] != value {
			t.Errorf("Mismatching value for key %s: %s != %s", key, result[key], value)
		}
	}
}

func Test_dbProfileConfig(t *testing.T) {
	var db *sql.DB
	var err error
	var result map[string]string
	var expected map[string]string

	db = createTestDb(t)
	defer db.Close()

	_, err = db.Exec("INSERT INTO profiles_config (profile_id, key, value) VALUES (3, 'something', 'something else');")

	result, err = dbProfileConfig(db, "theprofile")
	if err != nil {
		t.Fatal(err)
	}

	expected = map[string]string{"thekey": "thevalue", "something": "something else"}

	for key, value := range expected {
		if result[key] != value {
			t.Errorf("Mismatching value for key %s: %s != %s", key, result[key], value)
		}
	}
}

func Test_dbContainerProfiles(t *testing.T) {
	var db *sql.DB
	var err error
	var result []string
	var expected []string

	db = createTestDb(t)
	defer db.Close()

	expected = []string{"theprofile"}
	result, err = dbContainerProfiles(db, 1)
	if err != nil {
		t.Fatal(err)
	}

	for i := range expected {
		if expected[i] != result[i] {
			t.Fatal(fmt.Sprintf("Mismatching contents for profile list: %s != %s", result[i], expected[i]))
		}
	}
}

func Test_dbDevices_profiles(t *testing.T) {
	var db *sql.DB
	var err error
	var result shared.Devices
	var subresult shared.Device
	var expected shared.Device

	db = createTestDb(t)
	defer db.Close()

	result, err = dbDevices(db, "theprofile", true)
	if err != nil {
		t.Fatal(err)
	}

	expected = shared.Device{"type": "nic", "devicekey": "devicevalue"}
	subresult = result["devicename"]

	for key, value := range expected {
		if subresult[key] != value {
			t.Errorf("Mismatching value for key %s: %v != %v", key, subresult[key], value)
		}
	}

}

func Test_dbDevices_containers(t *testing.T) {
	var db *sql.DB
	var err error
	var result shared.Devices
	var subresult shared.Device
	var expected shared.Device

	db = createTestDb(t)
	defer db.Close()

	result, err = dbDevices(db, "thename", false)
	if err != nil {
		t.Fatal(err)
	}

	expected = shared.Device{"type": "nic", "configkey": "configvalue"}
	subresult = result["somename"]

	for key, value := range expected {
		if subresult[key] != value {
			t.Errorf("Mismatching value for key %s: %s != %s", key, subresult[key], value)
		}
	}

}