File size: 8,951 Bytes
329ce4a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import copy
import glob
import json
import os
from io import BytesIO
from pathlib import Path
from tempfile import TemporaryDirectory
from unittest import TestCase
from zipfile import ZipFile

from fastapi import HTTPException

from voicevox_engine.downloadable_library import LibraryManager

vvlib_manifest_name = "vvlib_manifest.json"


class TestLibraryManager(TestCase):
    def setUp(self):
        super().setUp()
        self.tmp_dir = TemporaryDirectory()
        self.tmp_dir_path = Path(self.tmp_dir.name)
        self.engine_name = "Test Engine"
        self.library_manger = LibraryManager(
            self.tmp_dir_path,
            "0.15.0",
            "Test",
            self.engine_name,
            "c7b58856-bd56-4aa1-afb7-b8415f824b06",
        )
        self.library_filename = Path("test/test.vvlib")
        with open("test/vvlib_manifest.json") as f:
            self.vvlib_manifest = json.loads(f.read())
            self.library_uuid = self.vvlib_manifest["uuid"]
        with ZipFile(self.library_filename, "w") as zf:
            speaker_infos = glob.glob("speaker_info/**", recursive=True)
            for info in speaker_infos:
                zf.write(info)
            zf.writestr(vvlib_manifest_name, json.dumps(self.vvlib_manifest))
        self.library_file = open(self.library_filename, "br")

    def tearDown(self):
        self.tmp_dir.cleanup()
        self.library_file.close()
        self.library_filename.unlink()

    def create_vvlib_without_manifest(self, filename: str):
        with ZipFile(filename, "w") as zf_out, ZipFile(
            self.library_filename, "r"
        ) as zf_in:
            for file in zf_in.infolist():
                buffer = zf_in.read(file.filename)
                if file.filename != vvlib_manifest_name:
                    zf_out.writestr(file, buffer)

    def create_vvlib_manifest(self, **kwargs):
        vvlib_manifest = copy.deepcopy(self.vvlib_manifest)
        return {**vvlib_manifest, **kwargs}

    def test_installed_libraries(self):
        self.assertEqual(self.library_manger.installed_libraries(), {})

        self.library_manger.install_library(
            self.library_uuid,
            self.library_file,
        )
        # 内容はdownloadable_library.jsonを元に生成されるので、内容は確認しない
        self.assertEqual(
            list(self.library_manger.installed_libraries().keys())[0], self.library_uuid
        )

        self.library_manger.uninstall_library(self.library_uuid)
        self.assertEqual(self.library_manger.installed_libraries(), {})

    def test_install_library(self):
        # エンジンが把握していないライブラリのテスト
        invalid_uuid = "52398bd5-3cc3-406c-a159-dfec5ace4bab"
        with self.assertRaises(HTTPException) as e:
            self.library_manger.install_library(invalid_uuid, self.library_file)
        self.assertEqual(e.exception.detail, f"指定された音声ライブラリ {invalid_uuid} が見つかりません。")

        # 不正なZIPファイルのテスト
        with self.assertRaises(HTTPException) as e:
            self.library_manger.install_library(self.library_uuid, BytesIO())
        self.assertEqual(e.exception.detail, f"音声ライブラリ {self.library_uuid} は不正なファイルです。")

        # vvlib_manifestの存在確認のテスト
        invalid_vvlib_name = "test/invalid.vvlib"
        self.create_vvlib_without_manifest(invalid_vvlib_name)
        with open(invalid_vvlib_name, "br") as f, self.assertRaises(HTTPException) as e:
            self.library_manger.install_library(self.library_uuid, f)
        self.assertEqual(
            e.exception.detail,
            f"指定された音声ライブラリ {self.library_uuid} にvvlib_manifest.jsonが存在しません。",
        )

        # vvlib_manifestのパースのテスト
        # Duplicate name: 'vvlib_manifest.json'とWarningを吐かれるので、毎回作り直す
        self.create_vvlib_without_manifest(invalid_vvlib_name)
        with ZipFile(invalid_vvlib_name, "a") as zf:
            zf.writestr(vvlib_manifest_name, "test")

        with open(invalid_vvlib_name, "br") as f, self.assertRaises(HTTPException) as e:
            self.library_manger.install_library(self.library_uuid, f)
        self.assertEqual(
            e.exception.detail,
            f"指定された音声ライブラリ {self.library_uuid} のvvlib_manifest.jsonは不正です。",
        )

        # vvlib_manifestのパースのテスト
        invalid_vvlib_manifest = self.create_vvlib_manifest(version=10)
        self.create_vvlib_without_manifest(invalid_vvlib_name)
        with ZipFile(invalid_vvlib_name, "a") as zf:
            zf.writestr(vvlib_manifest_name, json.dumps(invalid_vvlib_manifest))

        with open(invalid_vvlib_name, "br") as f, self.assertRaises(HTTPException) as e:
            self.library_manger.install_library(self.library_uuid, f)
        self.assertEqual(
            e.exception.detail,
            f"指定された音声ライブラリ {self.library_uuid} のvvlib_manifest.jsonに不正なデータが含まれています。",
        )

        # vvlib_manifestの不正なversionのテスト
        invalid_vvlib_manifest = self.create_vvlib_manifest(version="10")
        self.create_vvlib_without_manifest(invalid_vvlib_name)
        with ZipFile(invalid_vvlib_name, "a") as zf:
            zf.writestr(vvlib_manifest_name, json.dumps(invalid_vvlib_manifest))

        with open(invalid_vvlib_name, "br") as f, self.assertRaises(HTTPException) as e:
            self.library_manger.install_library(self.library_uuid, f)
        self.assertEqual(
            e.exception.detail, f"指定された音声ライブラリ {self.library_uuid} のversionが不正です。"
        )

        # vvlib_manifestの不正なmanifest_versionのテスト
        invalid_vvlib_manifest = self.create_vvlib_manifest(manifest_version="10")
        self.create_vvlib_without_manifest(invalid_vvlib_name)
        with ZipFile(invalid_vvlib_name, "a") as zf:
            zf.writestr(vvlib_manifest_name, json.dumps(invalid_vvlib_manifest))

        with open(invalid_vvlib_name, "br") as f, self.assertRaises(HTTPException) as e:
            self.library_manger.install_library(self.library_uuid, f)
        self.assertEqual(
            e.exception.detail,
            f"指定された音声ライブラリ {self.library_uuid} のmanifest_versionが不正です。",
        )

        # vvlib_manifestの未対応のmanifest_versionのテスト
        invalid_vvlib_manifest = self.create_vvlib_manifest(
            manifest_version="999.999.999"
        )
        self.create_vvlib_without_manifest(invalid_vvlib_name)
        with ZipFile(invalid_vvlib_name, "a") as zf:
            zf.writestr(vvlib_manifest_name, json.dumps(invalid_vvlib_manifest))

        with open(invalid_vvlib_name, "br") as f, self.assertRaises(HTTPException) as e:
            self.library_manger.install_library(self.library_uuid, f)
        self.assertEqual(
            e.exception.detail, f"指定された音声ライブラリ {self.library_uuid} は未対応です。"
        )

        # vvlib_manifestのインストール先エンジンの検証のテスト
        invalid_vvlib_manifest = self.create_vvlib_manifest(
            engine_uuid="26f7823b-20c6-40c5-bf86-6dd5d9d45c18"
        )
        self.create_vvlib_without_manifest(invalid_vvlib_name)
        with ZipFile(invalid_vvlib_name, "a") as zf:
            zf.writestr(vvlib_manifest_name, json.dumps(invalid_vvlib_manifest))

        with open(invalid_vvlib_name, "br") as f, self.assertRaises(HTTPException) as e:
            self.library_manger.install_library(self.library_uuid, f)
        self.assertEqual(
            e.exception.detail,
            f"指定された音声ライブラリ {self.library_uuid}{self.engine_name}向けではありません。",
        )

        # 正しいライブラリをインストールして問題が起きないか
        library_path = self.library_manger.install_library(
            self.library_uuid, self.library_file
        )
        self.assertEqual(self.tmp_dir_path / self.library_uuid, library_path)

        self.library_manger.uninstall_library(self.library_uuid)

        os.remove(invalid_vvlib_name)

    def test_uninstall_library(self):
        # TODO: アンインストール出来ないライブラリをテストできるようにしたい
        with self.assertRaises(HTTPException) as e:
            self.library_manger.uninstall_library(self.library_uuid)
        self.assertEqual(
            e.exception.detail, f"指定された音声ライブラリ {self.library_uuid} はインストールされていません。"
        )

        self.library_manger.install_library(self.library_uuid, self.library_file)
        self.library_manger.uninstall_library(self.library_uuid)